Skip to content
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

fix(rich-text-editor, editor): DLT-2175 editor loses text formatting while pasting #543

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ export default {
type: Array,
default: () => [],
},

/**
* Use default paste handler.
*/
useDefaultPasteHandler: {
type: Boolean,
default: false,
},
},

emits: [
Expand Down Expand Up @@ -530,43 +538,45 @@ export default {
to fix our issue of line breaks outputting as paragraphs. Code taken from this thread:
https://discuss.prosemirror.net/t/how-to-preserve-hard-breaks-when-pasting-html-into-a-plain-text-schema/4202/4
*/
handlePaste: function (view, event, slice) {
const { state } = view;
const { tr } = state;
...(!this.useDefaultPasteHandler && { handlePaste: this.handlerPreserveBreaksOnPaste }),
},
});
this.addEditorListeners();
},

if (!state.schema.nodes.hardBreak) {
return false;
}
handlerPreserveBreaksOnPaste (view, event, slice) {
const { state } = view;
const { tr } = state;

const clipboardText = event.clipboardData?.getData('text/plain').trim();
if (!state.schema.nodes.hardBreak) {
return false;
}

if (!clipboardText) {
return false;
}
const clipboardText = event.clipboardData?.getData('text/plain').trim();

const textLines = clipboardText.split(/(?:\r\n|\r|\n)/g);
if (!clipboardText) {
return false;
}

const nodes = textLines.reduce((nodes, line, index) => {
if (line.length > 0) {
nodes.push(state.schema.text(line));
}
const textLines = clipboardText.split(/(?:\r\n|\r|\n)/g);

if (index < textLines.length - 1) {
nodes.push(state.schema.nodes.hardBreak.create());
}
const nodes = textLines.reduce((nodes, line, index) => {
if (line.length > 0) {
nodes.push(state.schema.text(line));
}

return nodes;
}, []);
if (index < textLines.length - 1) {
nodes.push(state.schema.nodes.hardBreak.create());
}

view.dispatch(
tr.replaceSelection(Slice.maxOpen(Fragment.fromArray(nodes))).scrollIntoView(),
);
return nodes;
}, []);

return true;
},
},
});
this.addEditorListeners();
view.dispatch(
tr.replaceSelection(Slice.maxOpen(Fragment.fromArray(nodes))).scrollIntoView(),
);

return true;
},

processValue (newValue, returnIfEqual = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
:output-format="htmlOutputFormat"
:auto-focus="autoFocus"
:placeholder="placeholder"
:use-default-paste-handler="useDefaultPasteHandler"
:allow-line-breaks="true"
:link="true"
v-bind="$attrs"
Expand Down Expand Up @@ -458,6 +459,14 @@ export default {
setLinkInputAriaLabel: 'Input field to add link',
}),
},

/**
* Use default paste handler.
*/
useDefaultPasteHandler: {
type: Boolean,
default: false,
},
},

emits: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
:show-quote-button="$attrs.showQuoteButton"
:show-quick-replies-button="$attrs.showQuickRepliesButton"
:show-code-block-button="$attrs.showCodeBlockButton"
:use-default-paste-handler="$attrs.useDefaultPasteHandler"
@focus="$attrs.onFocus"
@blur="$attrs.onBlur"
@input="$attrs.onInput"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ export default {
type: Array,
default: () => [],
},

/**
* Use default paste handler.
*/
useDefaultPasteHandler: {
type: Boolean,
default: false,
},
},

emits: [
Expand Down Expand Up @@ -530,43 +538,45 @@ export default {
to fix our issue of line breaks outputting as paragraphs. Code taken from this thread:
https://discuss.prosemirror.net/t/how-to-preserve-hard-breaks-when-pasting-html-into-a-plain-text-schema/4202/4
*/
handlePaste: function (view, event, slice) {
const { state } = view;
const { tr } = state;
...(!this.useDefaultPasteHandler && { handlePaste: this.handlerPreserveBreaksOnPaste }),
},
});
this.addEditorListeners();
},

if (!state.schema.nodes.hardBreak) {
return false;
}
handlerPreserveBreaksOnPaste (view, event, slice) {
const { state } = view;
const { tr } = state;

const clipboardText = event.clipboardData?.getData('text/plain').trim();
if (!state.schema.nodes.hardBreak) {
return false;
}

if (!clipboardText) {
return false;
}
const clipboardText = event.clipboardData?.getData('text/plain').trim();

const textLines = clipboardText.split(/(?:\r\n|\r|\n)/g);
if (!clipboardText) {
return false;
}

const nodes = textLines.reduce((nodes, line, index) => {
if (line.length > 0) {
nodes.push(state.schema.text(line));
}
const textLines = clipboardText.split(/(?:\r\n|\r|\n)/g);

if (index < textLines.length - 1) {
nodes.push(state.schema.nodes.hardBreak.create());
}
const nodes = textLines.reduce((nodes, line, index) => {
if (line.length > 0) {
nodes.push(state.schema.text(line));
}

return nodes;
}, []);
if (index < textLines.length - 1) {
nodes.push(state.schema.nodes.hardBreak.create());
}

view.dispatch(
tr.replaceSelection(Slice.maxOpen(Fragment.fromArray(nodes))).scrollIntoView(),
);
return nodes;
}, []);

return true;
},
},
});
this.addEditorListeners();
view.dispatch(
tr.replaceSelection(Slice.maxOpen(Fragment.fromArray(nodes))).scrollIntoView(),
);

return true;
},

processValue (newValue, returnIfEqual = true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
:output-format="htmlOutputFormat"
:auto-focus="autoFocus"
:placeholder="placeholder"
:use-default-paste-handler="useDefaultPasteHandler"
:allow-line-breaks="true"
:link="true"
v-bind="$attrs"
Expand Down Expand Up @@ -461,6 +462,14 @@ export default {
setLinkInputAriaLabel: 'Input field to add link',
}),
},

/**
* Use default paste handler.
*/
useDefaultPasteHandler: {
type: Boolean,
default: false,
},
},

emits: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
:show-quote-button="$attrs.showQuoteButton"
:show-quick-replies-button="$attrs.showQuickRepliesButton"
:show-code-block-button="$attrs.showCodeBlockButton"
:use-default-paste-handler="$attrs.useDefaultPasteHandler"
@focus="$attrs.onFocus"
@blur="$attrs.onBlur"
@input="$attrs.onInput"
Expand Down
Loading