diff --git a/packages/core/src/toolmenu/index.tsx b/packages/core/src/toolmenu/index.tsx index 3eb5f41..7afc6b0 100644 --- a/packages/core/src/toolmenu/index.tsx +++ b/packages/core/src/toolmenu/index.tsx @@ -23,53 +23,44 @@ export const Toolmenu: React.FC = ({ path, onDone }) => { const handleCopyClick = useCallback(async () => { const selection = editor.selection; - if (!selection || selection.anchor.offset === selection.focus.offset) { - const textNodeEntryList = Array.from( - editor.nodes({ - at: path, - match: (node) => Text.isText(node), - }) - ); - if (!textNodeEntryList.length) { - return; - } - - const [lastTextNode, lastTextPath] = - textNodeEntryList[textNodeEntryList.length - 1]; - if (!Text.isText(lastTextNode)) { - return; - } + if (selection && selection.anchor.offset !== selection.focus.offset) { + helpers.nodeHelpers.copySelectedNodeToClipBoard(editor); + onDone(); + return; + } - const lastOffset = lastTextNode.text.length; - if (!lastOffset) { - onDone(); - return; - } + const [lastTextNode, lastTextPath] = editor.node(path, { edge: 'end' }); + if (!Text.isText(lastTextNode)) { + return; + } - const firstTextPath = textNodeEntryList[0][1]; - editor.selection = { - anchor: { - path: firstTextPath, - offset: 0, - }, - focus: { - path: lastTextPath, - offset: lastOffset, - }, - }; - await helpers.nodeHelpers.copySelectedNodeToClipBoard(editor); + const lastOffset = lastTextNode.text.length; + if (!lastOffset) { + onDone(); + return; + } - const headLinePoint = { - path: [...path, 0], + const [_, firstTextPath] = editor.node(path, { edge: 'start' }); + editor.selection = { + anchor: { + path: firstTextPath, offset: 0, - }; - editor.selection = { - anchor: headLinePoint, - focus: headLinePoint, - }; - } else { - helpers.nodeHelpers.copySelectedNodeToClipBoard(editor); - } + }, + focus: { + path: lastTextPath, + offset: lastOffset, + }, + }; + await helpers.nodeHelpers.copySelectedNodeToClipBoard(editor); + + const headLinePoint = { + path: firstTextPath, + offset: 0, + }; + editor.selection = { + anchor: headLinePoint, + focus: headLinePoint, + }; onDone(); }, [editor, path, onDone]);