This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
430 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
packages/editor/src/extensions/extensions/selection/decorations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Editor } from '@tiptap/core' | ||
import { EditorState, TextSelection, NodeSelection } from 'prosemirror-state' | ||
import { Decoration, DecorationSet } from 'prosemirror-view' | ||
import { SelectionOptions } from './meta' | ||
import { MultipleNodeSelection } from './MultipleNodeSelection' | ||
|
||
const DEFAULT_TEXT_SELECTION_CLASS = 'text-selection-highlight' | ||
const DEFAULT_NODE_SELECTION_CLASS = 'node-selection-highlight' | ||
|
||
export function textSelectionDecoration( | ||
editor: Editor, | ||
options: SelectionOptions, | ||
state: EditorState | ||
): DecorationSet | null { | ||
if (!editor.isEditable) return null | ||
|
||
if (!(state.selection instanceof TextSelection)) return null | ||
|
||
const { from, to } = state.selection | ||
if (from === to) return null | ||
|
||
const decorations: Decoration[] = [] | ||
|
||
const inlineDecoration = Decoration.inline(from, to, { | ||
class: options.textSelection?.className ?? DEFAULT_TEXT_SELECTION_CLASS, | ||
style: options.textSelection?.style | ||
}) | ||
decorations.push(inlineDecoration) | ||
|
||
return DecorationSet.create(editor.state.doc, decorations) | ||
} | ||
|
||
export function nodeSelectionDecoration( | ||
editor: Editor, | ||
options: SelectionOptions, | ||
state: EditorState | ||
): DecorationSet | null { | ||
if (!editor.isEditable) return null | ||
if (!(state.selection instanceof MultipleNodeSelection) && !(state.selection instanceof NodeSelection)) return null | ||
|
||
const decorations: Decoration[] = [] | ||
|
||
state.selection.ranges.forEach(range => { | ||
const nodeDecoration = Decoration.node(range.$from.pos, range.$to.pos, { | ||
class: options.nodeSelection?.className ?? DEFAULT_NODE_SELECTION_CLASS, | ||
style: options.nodeSelection?.style | ||
}) | ||
decorations.push(nodeDecoration) | ||
}) | ||
|
||
return DecorationSet.create(state.doc, decorations) | ||
} |
Oops, something went wrong.