Files
Tom Moor bf45e97641 chore: Enforce type import consistency (#10968)
* Update types

* fix circular dep

* type imports

* lint type imports and --fix
2025-12-19 23:07:02 -05:00

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;
}