Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

4.7 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type { IContextService } from '@univerjs/core';
import {
    EDITOR_ACTIVATED,
    FOCUSING_COMMON_DRAWINGS,
    FOCUSING_EDITOR_INPUT_FORMULA,
    FOCUSING_EDITOR_STANDALONE,
    FOCUSING_FX_BAR_EDITOR,
    FOCUSING_SHEET,
    FOCUSING_UNIVER_EDITOR,
} from '@univerjs/core';

export function whenSheetFocused(contextService: IContextService) {
    return contextService.getContextValue(FOCUSING_SHEET);
}

/**
 * Requires the currently focused unit to be Workbook and the sheet editor is focused but not activated.
 * @param contextService
 * @returns If the sheet editor is focused but not activated.
 */
export function whenSheetEditorFocused(contextService: IContextService): boolean {
    return (
        contextService.getContextValue(FOCUSING_SHEET) &&
        contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
        !contextService.getContextValue(EDITOR_ACTIVATED) &&
        !contextService.getContextValue(FOCUSING_COMMON_DRAWINGS)
    );
}

/**
 * Requires the currently focused unit to be Workbook, regardless of whether the cell editor is activated.
 * Used by inline-format shortcuts (bold/italic/underline/strikethrough) so they can also fire
 * inside the cell editor — the corresponding command will route to the docs inline-format command
 * when EDITOR_ACTIVATED is true.
 * @param contextService
 */
export function whenSheetFocusedInlineFormat(contextService: IContextService): boolean {
    return (
        contextService.getContextValue(FOCUSING_SHEET) &&
        contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
        !contextService.getContextValue(FOCUSING_COMMON_DRAWINGS)
    );
}

export function whenSheetEditorFocusedAndFxNotFocused(contextService: IContextService): boolean {
    return (
        contextService.getContextValue(FOCUSING_SHEET) &&
        contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
        !contextService.getContextValue(EDITOR_ACTIVATED) &&
        !contextService.getContextValue(FOCUSING_FX_BAR_EDITOR) &&
        !contextService.getContextValue(FOCUSING_COMMON_DRAWINGS)
    );
}

/**
 * Requires the currently focused unit to be Workbook and the sheet editor is activated.
 * @param contextService
 * @returns If the sheet editor is activated.
 */
export function whenSheetEditorActivated(contextService: IContextService) {
    return (
        contextService.getContextValue(FOCUSING_SHEET) &&
        contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
        contextService.getContextValue(EDITOR_ACTIVATED)
    );
}

export function whenEditorActivated(contextService: IContextService) {
    return contextService.getContextValue(FOCUSING_UNIVER_EDITOR) && contextService.getContextValue(EDITOR_ACTIVATED);
}

/**
 * Requires the currently focused editor is a formula editor.
 * @param contextService
 * @returns If the formula editor is focused.
 */
export function whenFormulaEditorFocused(contextService: IContextService) {
    return (
        contextService.getContextValue(FOCUSING_FX_BAR_EDITOR) &&
        contextService.getContextValue(FOCUSING_UNIVER_EDITOR)
    );
}

/**
 * Requires the currently focused editor is a formula editor, and it is activated.
 * @param contextService
 * @returns If the formula editor is activated.
 */
export function whenFormulaEditorActivated(contextService: IContextService) {
    return (
        contextService.getContextValue(FOCUSING_SHEET) &&
        contextService.getContextValue(EDITOR_ACTIVATED) &&
        contextService.getContextValue(FOCUSING_EDITOR_INPUT_FORMULA) &&
        contextService.getContextValue(FOCUSING_UNIVER_EDITOR)
    );
}

/**
 * Requires the currently focused editor is not a formula editor, and it is activated.
 * @param contextService
 * @returns If the editor is activated and the editor is not the formula editor.
 */
export function whenEditorDidNotInputFormulaActivated(contextService: IContextService) {
    return (
        contextService.getContextValue(FOCUSING_SHEET) &&
        contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
        contextService.getContextValue(EDITOR_ACTIVATED) &&
        !contextService.getContextValue(FOCUSING_EDITOR_INPUT_FORMULA) &&
        !contextService.getContextValue(FOCUSING_EDITOR_STANDALONE)
    );
}