1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Fixed copy, cut and paste in Rich Text editor (#3565)

This commit is contained in:
Laurent 2020-08-02 12:16:42 +01:00 committed by GitHub
parent 9f1a877f96
commit 4485947b0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 11 deletions

View File

@ -357,20 +357,24 @@ function AceEditor(props: NoteBodyEditorProps, ref: any) {
}, 10);
}, [props.content, editor, aceEditor_change]);
function clipboardText() {
return clipboard.readText() ? clipboard.readText() : clipboard.readHTML();
}
const editorCopyText = useCallback(() => {
const text = selectedText(selectionRange(editor), props.content);
clipboard.writeText(text);
}, [props.content, editor]);
const editorPasteText = useCallback(() => {
wrapSelectionWithStrings(clipboard.readText(), '', '', '');
wrapSelectionWithStrings(clipboardText(), '', '', '');
}, [wrapSelectionWithStrings]);
const onEditorContextMenu = useCallback(() => {
const menu = new Menu();
const hasSelectedText = !!selectedText(selectionRange(editor), props.content);
const clipboardText = clipboard.readText();
const currentClipboardText = clipboardText();
menu.append(
new MenuItem({
@ -397,7 +401,7 @@ function AceEditor(props: NoteBodyEditorProps, ref: any) {
label: _('Paste'),
enabled: true,
click: async () => {
if (clipboardText) {
if (currentClipboardText) {
editorPasteText();
} else {
// To handle pasting images

View File

@ -626,7 +626,6 @@ const TinyMCE = (props:NoteBodyEditorProps, ref:any) => {
update: function(element:any) {
let itemType:ContextMenuItemType = ContextMenuItemType.None;
let resourceId = '';
let textToCopy = '';
if (element.nodeName === 'IMG') {
itemType = ContextMenuItemType.Image;
@ -636,13 +635,20 @@ const TinyMCE = (props:NoteBodyEditorProps, ref:any) => {
itemType = resourceId ? ContextMenuItemType.Resource : ContextMenuItemType.Link;
} else {
itemType = ContextMenuItemType.Text;
textToCopy = editor.selection.getContent({ format: 'text' });
}
contextMenuActionOptions.current = { itemType, resourceId, textToCopy };
contextMenuActionOptions.current = {
itemType,
resourceId,
textToCopy: null,
htmlToCopy: editor.selection ? editor.selection.getContent() : '',
insertContent: (content:string) => {
editor.insertContent(content);
},
isReadOnly: false,
};
return item.isActive(itemType) ? itemNameNS : '';
return item.isActive(itemType, contextMenuActionOptions.current) ? itemNameNS : '';
},
});
}

View File

@ -21,6 +21,9 @@ export interface ContextMenuOptions {
itemType: ContextMenuItemType,
resourceId: string,
textToCopy: string,
htmlToCopy: string,
insertContent: Function,
isReadOnly?: boolean,
}
interface ContextMenuItem {
@ -81,12 +84,32 @@ export function menuItems():ContextMenuItems {
},
isActive: (itemType:ContextMenuItemType) => itemType === ContextMenuItemType.Image || itemType === ContextMenuItemType.Resource,
},
cut: {
label: _('Cut'),
onAction: async (options:ContextMenuOptions) => {
clipboard.writeText(options.textToCopy);
options.insertContent('');
},
isActive: (_itemType:ContextMenuItemType, options:ContextMenuOptions) => !options.isReadOnly && (!!options.textToCopy || !!options.htmlToCopy),
},
copy: {
label: _('Copy'),
onAction: async (options:ContextMenuOptions) => {
clipboard.writeText(options.textToCopy);
if (options.textToCopy) {
clipboard.writeText(options.textToCopy);
} else if (options.htmlToCopy) {
clipboard.writeHTML(options.htmlToCopy);
}
},
isActive: (itemType:ContextMenuItemType) => itemType === ContextMenuItemType.Text,
isActive: (_itemType:ContextMenuItemType, options:ContextMenuOptions) => !!options.textToCopy || !!options.htmlToCopy,
},
paste: {
label: _('Paste'),
onAction: async (options:ContextMenuOptions) => {
const content = clipboard.readHTML() ? clipboard.readHTML() : clipboard.readText();
options.insertContent(content);
},
isActive: (_itemType:ContextMenuItemType, options:ContextMenuOptions) => !options.isReadOnly && (!!clipboard.readText() || !!clipboard.readHTML()),
},
copyLinkUrl: {
label: _('Copy Link Address'),
@ -103,10 +126,12 @@ export default async function contextMenu(options:ContextMenuOptions) {
const items = menuItems();
if (!('readyOnly' in options)) options.isReadOnly = true;
for (const itemKey in items) {
const item = items[itemKey];
if (!item.isActive(options.itemType)) continue;
if (!item.isActive(options.itemType, options)) continue;
menu.append(new MenuItem({
label: item.label,

View File

@ -41,6 +41,8 @@ export default function useMessageHandler(scrollWhenReady:any, setScrollWhenRead
itemType: arg0 && arg0.type,
resourceId: arg0.resourceId,
textToCopy: arg0.textToCopy,
htmlToCopy: '',
insertContent: () => { console.warn('insertContent() not implemented'); },
});
menu.popup(bridge().window());