Skip to content
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 @@ -328,7 +328,7 @@ export function getItems(context, customItems = [], includeDefaultItems = true)
view.dispatch(tr.setSelection(SelectionType.create(doc, safeFrom, safeTo)));
}
}
const handled = html ? handleClipboardPaste({ editor, view }, html) : false;
const handled = handleClipboardPaste({ editor, view }, html, text);
if (!handled) {
const pasteEvent = createPasteEventShim({ html, text });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ describe('menuItems.js', () => {
expect(clipboardMocks.handleClipboardPaste).toHaveBeenCalledWith(
{ editor: mockEditor, view: mockEditor.view },
'<p>word html</p>',
'word html',
);
expect(mockEditor.view.pasteHTML).toHaveBeenCalledWith('<p>word html</p>', expect.any(Object));
expect(insertContent).not.toHaveBeenCalled();
Expand Down
6 changes: 5 additions & 1 deletion packages/super-editor/src/core/InputRule.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,8 @@ export function sanitizeHtml(html: string, forbiddenTags?: string[], domDocument
/**
* Handle clipboard paste events
*/
export function handleClipboardPaste(params: { editor: Editor; view: EditorView }, html: string): boolean;
export function handleClipboardPaste(
params: { editor: Editor; view: EditorView },
html: string,
plainText?: string,
): boolean;
28 changes: 21 additions & 7 deletions packages/super-editor/src/core/InputRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { isRegExp } from './utilities/isRegExp.js';
import { handleDocxPaste, wrapTextsInRuns } from './inputRules/docx-paste/docx-paste.js';
import { flattenListsInHtml } from './inputRules/html/html-helpers.js';
import { handleGoogleDocsHtml } from './inputRules/google-docs-paste/google-docs-paste.js';
import {
detectPasteUrl,
handlePlainTextUrlPaste,
normalizePastedLinks,
resolveLinkProtocols,
} from './inputRules/paste-link-normalizer.js';

export class InputRule {
match;
Expand Down Expand Up @@ -221,14 +227,15 @@ export const inputRulesPlugin = ({ editor, rules }) => {
handlePaste(view, event, slice) {
const clipboard = event.clipboardData;
const html = clipboard.getData('text/html');
const plainText = clipboard.getData('text/plain');

// Allow specialised plugins (e.g., field-annotation) first shot.
const fieldAnnotationContent = slice.content.content.filter((item) => item.type.name === 'fieldAnnotation');
if (fieldAnnotationContent.length) {
return false;
}

const result = handleClipboardPaste({ editor, view }, html);
const result = handleClipboardPaste({ editor, view }, html, plainText);
return result;
},
},
Expand Down Expand Up @@ -367,6 +374,7 @@ export function handleHtmlPaste(html, editor, source) {
// Extract the contents of the paragraph and paste only those
const paragraphContent = doc.firstChild.content;
const tr = state.tr.replaceSelectionWith(paragraphContent, false);
normalizePastedLinks(tr, editor);
dispatch(tr);
} else if (isInParagraph) {
// For multi-paragraph paste, use replaceSelection with a proper Slice
Expand All @@ -375,10 +383,13 @@ export function handleHtmlPaste(html, editor, source) {
const slice = new Slice(doc.content, 0, 0);

const tr = state.tr.replaceSelection(slice);
normalizePastedLinks(tr, editor);
dispatch(tr);
} else {
// Use the original behavior for other cases
dispatch(state.tr.replaceSelectionWith(doc, true));
const tr = state.tr.replaceSelectionWith(doc, true);
normalizePastedLinks(tr, editor);
dispatch(tr);
}

return true;
Expand Down Expand Up @@ -483,9 +494,10 @@ export function sanitizeHtml(html, forbiddenTags = ['meta', 'svg', 'script', 'st
* @param {Editor} params.editor The SuperEditor instance.
* @param {View} params.view The ProseMirror view associated with the editor.
* @param {String} html HTML clipboard content (may be empty).
* @param {String} [plainText] Plain-text clipboard content (may be empty).
* @returns {Boolean} Whether the paste was handled.
*/
export function handleClipboardPaste({ editor, view }, html) {
export function handleClipboardPaste({ editor, view }, html, plainText) {
let source;

if (!html) {
Expand All @@ -499,10 +511,12 @@ export function handleClipboardPaste({ editor, view }, html) {
}

switch (source) {
case 'plain-text':
// Let native/plain text paste fall through so ProseMirror handles it.
// Will hit the Fallback when boolean is returned false
return false;
case 'plain-text': {
const protocols = resolveLinkProtocols(editor);
const detected = detectPasteUrl(plainText, protocols);
if (!detected) return false;
return handlePlainTextUrlPaste(editor, view, plainText, detected);
}
case 'word-html':
if (editor.options.mode === 'docx') {
return handleDocxPaste(html, editor, view);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DOMParser, Fragment } from 'prosemirror-model';
import { cleanHtmlUnnecessaryTags, convertEmToPt, handleHtmlPaste } from '../../InputRule.js';
import { normalizePastedLinks } from '../paste-link-normalizer.js';
import { ListHelpers } from '@helpers/list-numbering-helpers.js';
import {
extractListLevelStyles,
Expand Down Expand Up @@ -188,7 +189,9 @@ export const handleDocxPaste = (html, editor, view) => {
const { dispatch } = editor.view;
if (!dispatch) return false;

dispatch(view.state.tr.replaceSelectionWith(doc, true));
const tr = view.state.tr.replaceSelectionWith(doc, true);
normalizePastedLinks(tr, editor);
dispatch(tr);
return true;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DOMParser } from 'prosemirror-model';
import { convertEmToPt, sanitizeHtml } from '../../InputRule.js';
import { normalizePastedLinks } from '../paste-link-normalizer.js';
import { ListHelpers } from '../../helpers/list-numbering-helpers.js';
import { createSingleItemList } from '../html/html-helpers.js';
import { getLvlTextForGoogleList, googleNumDefMap } from '../../helpers/pasteListHelpers.js';
Expand Down Expand Up @@ -32,7 +33,9 @@ export const handleGoogleDocsHtml = (html, editor, view) => {
const { dispatch } = editor.view;
if (!dispatch) return false;

dispatch(view.state.tr.replaceSelectionWith(doc, true));
const tr = view.state.tr.replaceSelectionWith(doc, true);
normalizePastedLinks(tr, editor);
dispatch(tr);
return true;
};

Expand Down
Loading
Loading