Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix white spaces not being preserved when pasted into editor
Browse files Browse the repository at this point in the history
luin committed Jul 17, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 5d752e3 commit 5e612f4
Showing 1 changed file with 40 additions and 7 deletions.
47 changes: 40 additions & 7 deletions packages/quill/src/modules/clipboard.ts
Original file line number Diff line number Diff line change
@@ -365,14 +365,47 @@ function isBetweenInlineElements(node: HTMLElement, scroll: ScrollBlot) {
}

const preNodes = new WeakMap();
function isPre(node: Node | null) {

enum WhiteSpaceStyle {
Collapsed,
Preserved,
Inherited,
}
const getWhiteSpaceStyle = (node: Node) => {
if (node instanceof HTMLElement) {
const { whiteSpace } = node.style;
if (
whiteSpace === 'pre' ||
whiteSpace === 'pre-wrap' ||
whiteSpace === 'break-spaces'
) {
return WhiteSpaceStyle.Preserved;
}
if (whiteSpace && whiteSpace !== 'inherit') {
return WhiteSpaceStyle.Collapsed;
}
if (node.tagName === 'PRE') {
return WhiteSpaceStyle.Preserved;
}
}
return WhiteSpaceStyle.Inherited;
};

function shouldPreserveWhiteSpaces(node: Node | null) {
if (node == null) return false;

if (!preNodes.has(node)) {
// @ts-expect-error
if (node.tagName === 'PRE') {
preNodes.set(node, true);
} else {
preNodes.set(node, isPre(node.parentNode));
const type = getWhiteSpaceStyle(node);
switch (type) {
case WhiteSpaceStyle.Preserved:
preNodes.set(node, true);
break;
case WhiteSpaceStyle.Collapsed:
preNodes.set(node, false);
break;
case WhiteSpaceStyle.Inherited:
preNodes.set(node, shouldPreserveWhiteSpaces(node.parentNode));
break;
}
}
return preNodes.get(node);
@@ -631,7 +664,7 @@ function matchText(node: HTMLElement, delta: Delta, scroll: ScrollBlot) {
if (node.parentElement?.tagName === 'O:P') {
return delta.insert(text.trim());
}
if (!isPre(node)) {
if (!shouldPreserveWhiteSpaces(node)) {
if (
text.trim().length === 0 &&
text.includes('\n') &&

0 comments on commit 5e612f4

Please sign in to comment.