mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import type { EditorState } from "prosemirror-state";
|
|
import { isMarkActive } from "./isMarkActive";
|
|
import { isNodeActive } from "./isNodeActive";
|
|
|
|
type Options = {
|
|
/** Only check if the selection is inside a code block. */
|
|
onlyBlock?: boolean;
|
|
/** Only check if the selection is inside a code mark. */
|
|
onlyMark?: boolean;
|
|
/** If true then code must contain entire selection */
|
|
inclusive?: boolean;
|
|
};
|
|
|
|
/**
|
|
* Returns true if the selection is inside a code block or code mark.
|
|
*
|
|
* @param state The editor state.
|
|
* @param options The options.
|
|
* @returns True if the selection is inside a code block or code mark.
|
|
*/
|
|
export function isInCode(state: EditorState, options?: Options): boolean {
|
|
const { nodes, marks } = state.schema;
|
|
const opts =
|
|
options?.inclusive !== undefined
|
|
? { inclusive: options?.inclusive }
|
|
: undefined;
|
|
|
|
if (!options?.onlyMark) {
|
|
if (
|
|
nodes.code_block &&
|
|
isNodeActive(nodes.code_block, undefined, opts)(state)
|
|
) {
|
|
return true;
|
|
}
|
|
if (
|
|
nodes.code_fence &&
|
|
isNodeActive(nodes.code_fence, undefined, opts)(state)
|
|
) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (!options?.onlyBlock) {
|
|
if (marks.code_inline) {
|
|
return isMarkActive(marks.code_inline, undefined, opts)(state);
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|