Skip to content
Open
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
21 changes: 10 additions & 11 deletions packages/super-editor/src/components/slash-menu/menuItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import TableActions from '../toolbar/TableActions.vue';
import LinkInput from '../toolbar/LinkInput.vue';
import { TEXTS, ICONS, TRIGGERS } from './constants.js';
import { isTrackedChangeActionAllowed } from '@extensions/track-changes/permission-helpers.js';
import { readClipboardRaw } from '../../core/utilities/clipboardUtils.js';
import { handleClipboardPaste } from '../../core/InputRule.js';

/**
* Check if a module is enabled based on editor options
Expand Down Expand Up @@ -257,17 +259,14 @@ export function getItems(context, customItems = [], includeDefaultItems = true)
label: TEXTS.paste,
icon: ICONS.paste,
isDefault: true,
action: (editor) => {
// Use execCommand('paste') - triggers native paste without permission prompt
// This works because it's triggered by user interaction (clicking the menu item)
const editorDom = editor.view?.dom;
if (editorDom) {
editorDom.focus();
// execCommand paste is allowed when triggered by user action
const success = document.execCommand('paste');
if (!success) {
console.warn('[Paste] execCommand paste failed - clipboard may be empty or inaccessible');
}
action: async (editor) => {
const { view } = editor ?? {};
if (!view) return;
view.dom.focus();
const { html, text } = await readClipboardRaw();
const handled = html ? handleClipboardPaste({ editor, view }, html) : false;
if (!handled && text && editor.commands?.insertContent) {
editor.commands.insertContent(text, { contentType: 'text' });
}
},
showWhen: (context) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/super-editor/src/core/utilities/clipboardUtils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* @returns {Promise<boolean>} Whether clipboard read permission is granted
*/
export function ensureClipboardPermission(): Promise<boolean>;
/**
* Reads raw HTML and text from the system clipboard (for use in paste actions).
*/
export function readClipboardRaw(): Promise<{ html: string; text: string }>;
/**
* Reads content from the system clipboard and parses it into a ProseMirror fragment.
* Attempts to read HTML first, falling back to plain text if necessary.
Expand Down
48 changes: 30 additions & 18 deletions packages/super-editor/src/core/utilities/clipboardUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,36 +53,48 @@ export async function ensureClipboardPermission() {
}

/**
* Reads content from the system clipboard and parses it into a ProseMirror fragment.
* Attempts to read HTML first, falling back to plain text if necessary.
* @param {EditorState} state - The ProseMirror editor state, used for schema and parsing.
* @returns {Promise<Fragment|ProseMirrorNode|null>} A promise that resolves to a ProseMirror fragment or text node, or null if reading fails.
* Reads raw HTML and text from the system clipboard (for use in paste actions).
* @returns {Promise<{ html: string, text: string }>}
*/
export async function readFromClipboard(state) {
export async function readClipboardRaw() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 [Review Bot: Major] This function duplicates clipboard reading logic from readFromClipboard() (lines 96-120). Both check permissions, try read() for HTML/text, and fall back to readText().

Consider refactoring readFromClipboard to use this function internally:

export async function readFromClipboard(state) {
  const { html, text } = await readClipboardRaw();
  // ... parse html/text to ProseMirror content
}

This reduces maintenance burden and ensures both paths stay in sync.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying out this review bot.. ignore if not helpful. seems worth looking at though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a fix

let html = '';
let text = '';
const hasPermission = await ensureClipboardPermission();

if (hasPermission && navigator.clipboard && navigator.clipboard.read) {
try {
const items = await navigator.clipboard.read();
for (const item of items) {
if (item.types.includes('text/html')) {
html = await (await item.getType('text/html')).text();
break;
} else if (item.types.includes('text/plain')) {
text = await (await item.getType('text/plain')).text();
if (hasPermission && navigator.clipboard) {
if (navigator.clipboard.read) {
try {
const items = await navigator.clipboard.read();
for (const item of items) {
if (item.types.includes('text/html')) {
html = await (await item.getType('text/html')).text();
}
if (item.types.includes('text/plain')) {
text = await (await item.getType('text/plain')).text();
}
}
} catch {
try {
text = await navigator.clipboard.readText();
} catch {}
}
} catch {
// Fallback to plain text read; may still fail if permission denied
} else {
try {
text = await navigator.clipboard.readText();
} catch {}
}
} else {
// permissions denied or API unavailable; leave content empty
}
return { html, text: text || '' };
}

/**
* Reads content from the system clipboard and parses it into a ProseMirror fragment.
* Attempts to read HTML first, falling back to plain text if necessary.
* @param {EditorState} state - The ProseMirror editor state, used for schema and parsing.
* @returns {Promise<Fragment|ProseMirrorNode|null>} A promise that resolves to a ProseMirror fragment or text node, or null if reading fails.
*/
export async function readFromClipboard(state) {
const { html, text } = await readClipboardRaw();
let content = null;
if (html) {
try {
Expand Down