Skip to content

Commit 320bc10

Browse files
authored
Merge pull request #15 from yandex-cloud/selection-fix
feat: added new field for selection interface
2 parents 01ad8a8 + 4ccfdaa commit 320bc10

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/selection.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import prosemirrorState from 'prosemirror-state';
2+
3+
declare module 'prosemirror-state' {
4+
class Selection extends prosemirrorState.Selection {
5+
selectionName?: string;
6+
}
7+
}

src/utils/selection.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import {Node, NodeType} from 'prosemirror-model';
22
import {Selection, TextSelection, NodeSelection, AllSelection} from 'prosemirror-state';
33

4+
NodeSelection.prototype.selectionName = 'NodeSelection';
5+
TextSelection.prototype.selectionName = 'TextSelection';
6+
AllSelection.prototype.selectionName = 'AllSelection';
7+
48
export const isTextSelection = (selection: Selection): selection is TextSelection =>
5-
// for some reason "instanceof" sometimes returns false
6-
selection.constructor.name === TextSelection.prototype.constructor.name;
9+
selection.selectionName === TextSelection.prototype.selectionName;
710

811
export const isNodeSelection = (selection: Selection): selection is NodeSelection =>
9-
// for some reason "instanceof" sometimes returns false
10-
selection.constructor.name === NodeSelection.prototype.constructor.name;
12+
selection.selectionName === NodeSelection.prototype.selectionName;
1113

1214
// ts broke down when use "selection is AllSelection" return type
1315
// maybe because AllSelection has same class type with different constructor
1416
export const isWholeSelection = (selection: Selection): boolean =>
15-
// for some reason "instanceof" sometimes returns false
16-
selection.constructor.name === AllSelection.prototype.constructor.name;
17+
selection.selectionName === AllSelection.prototype.selectionName;
1718

1819
export const equalNodeType = function equalNodeType(nodeType: NodeType[] | NodeType, node: Node) {
1920
return (Array.isArray(nodeType) && nodeType.indexOf(node.type) > -1) || node.type === nodeType;

0 commit comments

Comments
 (0)