From 4ccfdaa2f5c5f6e27c89d87181f1d6c615888c9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A2=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=B8=D0=BD=D1=86=D0=B5=D0=B2?= Date: Wed, 5 Oct 2022 10:54:10 +0600 Subject: [PATCH] feat: added new field for selection interface --- src/selection.d.ts | 7 +++++++ src/utils/selection.ts | 13 +++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 src/selection.d.ts 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;