-
Notifications
You must be signed in to change notification settings - Fork 62
[WC-3149][WC-3109]: tab keypress and softbreak on rich text #1991
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
gjulivan
wants to merge
4
commits into
main
Choose a base branch
from
rte-update
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
5 changes: 3 additions & 2 deletions
5
.../pluggableWidgets/rich-text-web/src/utils/formats/quill-table-better/modules/clipboard.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
176 changes: 139 additions & 37 deletions
176
packages/pluggableWidgets/rich-text-web/src/utils/modules/clipboard.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 |
|---|---|---|
| @@ -1,48 +1,150 @@ | ||
| /* | ||
| * Custom Clipboard module to override Quill's default clipboard behavior | ||
| * to better handle pasting from various sources. | ||
| * https://github.com/slab/quill/blob/main/packages/quill/src/modules/clipboard.ts | ||
| */ | ||
|
|
||
| import { EmbedBlot, type ScrollBlot } from "parchment"; | ||
| import Quill, { Delta } from "quill"; | ||
| import Clipboard from "quill/modules/clipboard"; | ||
| import Clipboard, { matchNewline } from "quill/modules/clipboard"; | ||
|
|
||
| export default class CustomClipboard extends Clipboard { | ||
| constructor(quill: Quill, options: any) { | ||
| super(quill, options); | ||
| // remove default CLIPBOARD_CONFIG list matchers for ol and ul | ||
| // https://github.com/slab/quill/blob/539cbffd0a13b18e9c65eb84dd35e6596e403158/packages/quill/src/modules/clipboard.ts#L32 | ||
| this.matchers = this.matchers.filter(matcher => matcher[0] !== "ol, ul" && matcher[0] !== Node.TEXT_NODE); | ||
| // adding back, we do not actually want to remove newline matching | ||
| this.matchers.unshift([Node.TEXT_NODE, matchNewline]); | ||
| // add custom text matcher to better handle spaces and newlines | ||
| this.matchers.unshift([Node.TEXT_NODE, customMatchText]); | ||
| // add custom list matchers for ol and ul to allow custom list types (lower-alpha, lower-roman, etc.) | ||
| this.addMatcher("ol, ul", matchList); | ||
| } | ||
| } | ||
|
|
||
| // remove default list matchers for ol and ul | ||
| this.matchers = this.matchers.filter(matcher => matcher[0] !== "ol, ul"); | ||
| function isLine(node: Node, scroll: ScrollBlot): any { | ||
| if (!(node instanceof Element)) return false; | ||
| const match = scroll.query(node); | ||
| // @ts-expect-error prototype not exist on match | ||
| if (match && match.prototype instanceof EmbedBlot) return false; | ||
|
|
||
| // add custom list matchers for ol and ul to allow custom list types (lower-alpha, lower-roman, etc.) | ||
| this.addMatcher("ol, ul", (node, delta) => { | ||
| const format = "list"; | ||
| let list = "ordered"; | ||
| const element = node as HTMLUListElement; | ||
| const checkedAttr = element.getAttribute("data-checked"); | ||
| if (checkedAttr) { | ||
| list = checkedAttr === "true" ? "checked" : "unchecked"; | ||
| return [ | ||
| "address", | ||
| "article", | ||
| "blockquote", | ||
| "canvas", | ||
| "dd", | ||
| "div", | ||
| "dl", | ||
| "dt", | ||
| "fieldset", | ||
| "figcaption", | ||
| "figure", | ||
| "footer", | ||
| "form", | ||
| "h1", | ||
| "h2", | ||
| "h3", | ||
| "h4", | ||
| "h5", | ||
| "h6", | ||
| "header", | ||
| "iframe", | ||
| "li", | ||
| "main", | ||
| "nav", | ||
| "ol", | ||
| "output", | ||
| "p", | ||
| "pre", | ||
| "section", | ||
| "table", | ||
| "td", | ||
| "tr", | ||
| "ul", | ||
| "video" | ||
| ].includes(node.tagName.toLowerCase()); | ||
|
Comment on lines
+32
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have maybe a const here? like |
||
| } | ||
|
|
||
| function isBetweenInlineElements(node: HTMLElement, scroll: ScrollBlot): boolean | null { | ||
| return ( | ||
| node.previousElementSibling && | ||
| node.nextElementSibling && | ||
| !isLine(node.previousElementSibling, scroll) && | ||
| !isLine(node.nextElementSibling, scroll) | ||
| ); | ||
| } | ||
|
|
||
| const preNodes = new WeakMap(); | ||
| function isPre(node: Node | null): boolean { | ||
| if (node == null) return false; | ||
| if (!preNodes.has(node)) { | ||
| // @ts-expect-error tagName not exist on Node | ||
| if (node.tagName === "PRE") { | ||
| preNodes.set(node, true); | ||
| } else { | ||
| preNodes.set(node, isPre(node.parentNode)); | ||
| } | ||
| } | ||
| return preNodes.get(node); | ||
| } | ||
|
|
||
| function customMatchText(node: HTMLElement, delta: Delta, scroll: ScrollBlot): Delta { | ||
| // @ts-expect-error data not exist on node | ||
| let text = node.data as string; | ||
| // Word represents empty line with <o:p> </o:p> | ||
| if (node.parentElement?.tagName === "O:P") { | ||
| return delta.insert(text.trim()); | ||
| } | ||
| if (!isPre(node)) { | ||
| if (text.trim().length === 0 && text.includes("\n") && !isBetweenInlineElements(node, scroll)) { | ||
| return delta; | ||
| } | ||
| // collapse consecutive spaces into one | ||
| text = text.replace(/ {2,}/g, " "); | ||
| if ( | ||
| (node.nextSibling == null && node.parentElement != null && isLine(node.parentElement, scroll)) || | ||
| (node.nextSibling instanceof Element && isLine(node.nextSibling, scroll)) | ||
| ) { | ||
| // block structure means we don't need trailing space | ||
| text = text.replace(/ $/, ""); | ||
| } | ||
| } | ||
| return delta.insert(text); | ||
| } | ||
|
|
||
| function matchList(node: HTMLElement, delta: Delta): Delta { | ||
| const format = "list"; | ||
| let list = "ordered"; | ||
| const element = node as HTMLUListElement; | ||
| const checkedAttr = element.getAttribute("data-checked"); | ||
| if (checkedAttr) { | ||
| list = checkedAttr === "true" ? "checked" : "unchecked"; | ||
| } else { | ||
| const listStyleType = element.style.listStyleType; | ||
| if (listStyleType) { | ||
| if (listStyleType === "disc") { | ||
| // disc is standard list type, convert to bullet | ||
| list = "bullet"; | ||
| } else if (listStyleType === "decimal") { | ||
| // list decimal type is dependant on indent level, convert to standard ordered list | ||
| list = "ordered"; | ||
| } else { | ||
| const listStyleType = element.style.listStyleType; | ||
| if (listStyleType) { | ||
| if (listStyleType === "disc") { | ||
| // disc is standard list type, convert to bullet | ||
| list = "bullet"; | ||
| } else if (listStyleType === "decimal") { | ||
| // list decimal type is dependant on indent level, convert to standard ordered list | ||
| list = "ordered"; | ||
| } else { | ||
| list = listStyleType; | ||
| } | ||
| } else { | ||
| list = element.tagName === "OL" ? "ordered" : "bullet"; | ||
| } | ||
| list = listStyleType; | ||
| } | ||
|
|
||
| // apply list format to delta | ||
| return delta.reduce((newDelta, op) => { | ||
| if (!op.insert) return newDelta; | ||
| if (op.attributes && op.attributes[format]) { | ||
| return newDelta.push(op); | ||
| } | ||
| const formats = list ? { [format]: list } : {}; | ||
|
|
||
| return newDelta.insert(op.insert, { ...formats, ...op.attributes }); | ||
| }, new Delta()); | ||
| }); | ||
| } else { | ||
| list = element.tagName === "OL" ? "ordered" : "bullet"; | ||
| } | ||
| } | ||
|
|
||
| // apply list format to delta | ||
| return delta.reduce((newDelta, op) => { | ||
| if (!op.insert) return newDelta; | ||
| if (op.attributes && op.attributes[format]) { | ||
| return newDelta.push(op); | ||
| } | ||
| const formats = list ? { [format]: list } : {}; | ||
| return newDelta.insert(op.insert, { ...formats, ...op.attributes }); | ||
| }, new Delta()); | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe remove if the plan is to not use it anymore in the future