-
-
Notifications
You must be signed in to change notification settings - Fork 554
feat: Selectable non-editable text in blocks without inline content #1087
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matthewlipski
wants to merge
5
commits into
main
Choose a base branch
from
node-text-selection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6585bcd
Made it possible to select text in blocks without inline content
matthewlipski 408e3de
fix bug?
YousefED 38093f2
Implemented PR feedback & cleaned up code
matthewlipski 7072083
- Refactored classes and CSS
matthewlipski a90a57d
Merge remote-tracking branch 'origin/main' into node-text-selection
YousefED File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,77 @@ | ||
/* From https://github.com/ueberdosis/tiptap/blob/a170cf4057de98d0350e318c51e57e2998fac38e/packages/core/src/style.ts */ | ||
.ProseMirror { | ||
position: relative; | ||
} | ||
|
||
.ProseMirror { | ||
word-wrap: break-word; | ||
white-space: pre-wrap; | ||
white-space: break-spaces; | ||
-webkit-font-variant-ligatures: none; | ||
font-variant-ligatures: none; | ||
font-feature-settings: "liga" 0; /* the above doesn't seem to work in Edge */ | ||
} | ||
|
||
.ProseMirror [contenteditable="false"] { | ||
white-space: normal; | ||
} | ||
|
||
.ProseMirror [contenteditable="false"] [contenteditable="true"] { | ||
white-space: pre-wrap; | ||
} | ||
|
||
.ProseMirror pre { | ||
white-space: pre-wrap; | ||
} | ||
|
||
img.ProseMirror-separator { | ||
display: inline !important; | ||
border: none !important; | ||
margin: 0 !important; | ||
width: 1px !important; | ||
height: 1px !important; | ||
} | ||
|
||
.ProseMirror-gapcursor { | ||
display: none; | ||
pointer-events: none; | ||
position: absolute; | ||
margin: 0; | ||
} | ||
|
||
.ProseMirror-gapcursor:after { | ||
content: ""; | ||
display: block; | ||
position: absolute; | ||
top: -2px; | ||
width: 20px; | ||
border-top: 1px solid black; | ||
animation: ProseMirror-cursor-blink 1.1s steps(2, start) infinite; | ||
} | ||
|
||
@keyframes ProseMirror-cursor-blink { | ||
to { | ||
visibility: hidden; | ||
} | ||
} | ||
|
||
/* Edited section to replace `.ProseMirror-hideselection` with `.ProseMirror-fullyselected */ | ||
.ProseMirror-fullyselected *::selection { | ||
matthewlipski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
background: transparent; | ||
} | ||
|
||
.ProseMirror-fullyselected *::-moz-selection { | ||
background: transparent; | ||
} | ||
|
||
.ProseMirror-fullyselected * { | ||
caret-color: transparent; | ||
} | ||
|
||
.ProseMirror-focused .ProseMirror-gapcursor { | ||
display: block; | ||
} | ||
|
||
.tippy-box[data-animation=fade][data-state=hidden] { | ||
opacity: 0 | ||
} |
72 changes: 72 additions & 0 deletions
72
packages/core/src/extensions/FullySelectedNode/FullySelectedNodeExtension.ts
This file contains hidden or 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,72 @@ | ||
import { Editor, Extension } from "@tiptap/core"; | ||
import { getBlockInfoFromPos } from "../../api/getBlockInfoFromPos"; | ||
|
||
// Removes the `ProseMirror-fullyselected` class name from the editor when a | ||
// NodeSelection is active on a block without inline content, but the DOM | ||
// selection is within the node, rather than fully wrapping it. These 2 | ||
// scenarios look identical in the editor state, so we need to check the DOM | ||
// selection to differentiate them. | ||
const onSelectionChange = (editor: Editor) => { | ||
const selection = document.getSelection(); | ||
if (selection === null) { | ||
return; | ||
} | ||
|
||
// selectionchange events don't bubble, so we have to scope them in this way | ||
// instead of setting the listener on the editor element. | ||
if ( | ||
!editor.view.dom.contains(selection.anchorNode) || | ||
!editor.view.dom.contains(selection.focusNode) | ||
) { | ||
return; | ||
} | ||
|
||
// Node selection is active. | ||
const isNodeSelection = "node" in editor.state.selection; | ||
if (!isNodeSelection) { | ||
matthewlipski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
editor.view.dom.classList.remove("ProseMirror-fullyselected"); | ||
return; | ||
} | ||
|
||
const blockInfo = getBlockInfoFromPos( | ||
editor.state.doc, | ||
editor.state.selection.from | ||
); | ||
|
||
// Selected block has no inline content. | ||
const selectedNodeHasNoContent = | ||
matthewlipski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
blockInfo.contentNode.type.spec.content === ""; | ||
if (!selectedNodeHasNoContent) { | ||
editor.view.dom.classList.remove("ProseMirror-fullyselected"); | ||
return; | ||
} | ||
|
||
const blockElement = editor.view.domAtPos(blockInfo.startPos).node; | ||
|
||
if ( | ||
// Selection doesn't wrap this node. | ||
selection.type !== "Range" || | ||
selection.anchorNode !== blockElement || | ||
selection.focusNode !== blockElement || | ||
selection.anchorOffset !== 0 || | ||
selection.focusOffset !== 1 | ||
) { | ||
editor.view.dom.classList.remove("ProseMirror-fullyselected"); | ||
} else if (!editor.view.dom.classList.contains("ProseMirror-fullyselected")) { | ||
editor.view.dom.classList.add("ProseMirror-fullyselected"); | ||
} | ||
}; | ||
|
||
export const FullySelectedNodeExtension = Extension.create({ | ||
name: "fullySelectedNode", | ||
onCreate() { | ||
document.addEventListener("selectionchange", () => { | ||
onSelectionChange(this.editor); | ||
}); | ||
}, | ||
onDestroy() { | ||
document.removeEventListener("selectionchange", () => { | ||
onSelectionChange(this.editor); | ||
matthewlipski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
}, | ||
}); |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.