diff --git a/src/selection.d.ts b/src/selection.d.ts new file mode 100644 index 00000000..f116c51d --- /dev/null +++ b/src/selection.d.ts @@ -0,0 +1,7 @@ +import prosemirrorState from 'prosemirror-state'; + +declare module 'prosemirror-state' { + class Selection extends prosemirrorState.Selection { + selectionName?: string; + } +} diff --git a/src/utils/selection.ts b/src/utils/selection.ts index b532f89c..60b290b5 100644 --- a/src/utils/selection.ts +++ b/src/utils/selection.ts @@ -1,19 +1,20 @@ import {Node, NodeType} from 'prosemirror-model'; import {Selection, TextSelection, NodeSelection, AllSelection} from 'prosemirror-state'; +NodeSelection.prototype.selectionName = 'NodeSelection'; +TextSelection.prototype.selectionName = 'TextSelection'; +AllSelection.prototype.selectionName = 'AllSelection'; + export const isTextSelection = (selection: Selection): selection is TextSelection => - // for some reason "instanceof" sometimes returns false - selection.constructor.name === TextSelection.prototype.constructor.name; + selection.selectionName === TextSelection.prototype.selectionName; export const isNodeSelection = (selection: Selection): selection is NodeSelection => - // for some reason "instanceof" sometimes returns false - selection.constructor.name === NodeSelection.prototype.constructor.name; + selection.selectionName === NodeSelection.prototype.selectionName; // ts broke down when use "selection is AllSelection" return type // maybe because AllSelection has same class type with different constructor export const isWholeSelection = (selection: Selection): boolean => - // for some reason "instanceof" sometimes returns false - selection.constructor.name === AllSelection.prototype.constructor.name; + selection.selectionName === AllSelection.prototype.selectionName; export const equalNodeType = function equalNodeType(nodeType: NodeType[] | NodeType, node: Node) { return (Array.isArray(nodeType) && nodeType.indexOf(node.type) > -1) || node.type === nodeType;