Skip to content

Commit

Permalink
refactor: refactor handleCopyClick function ♻️
Browse files Browse the repository at this point in the history
  • Loading branch information
MasanobuRyuman committed Jun 20, 2023
1 parent a68b0ae commit 3f9b547
Showing 1 changed file with 34 additions and 43 deletions.
77 changes: 34 additions & 43 deletions packages/core/src/toolmenu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,44 @@ export const Toolmenu: React.FC<ToolmenuProps> = ({ 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]);

Expand Down

0 comments on commit 3f9b547

Please sign in to comment.