Skip to content

Commit

Permalink
feat: added new field for selection interface
Browse files Browse the repository at this point in the history
  • Loading branch information
smsochneg committed Oct 5, 2022
1 parent 52c5d3b commit 4ccfdaa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/selection.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import prosemirrorState from 'prosemirror-state';

declare module 'prosemirror-state' {
class Selection extends prosemirrorState.Selection {
selectionName?: string;
}
}
13 changes: 7 additions & 6 deletions src/utils/selection.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 4ccfdaa

Please sign in to comment.