Skip to content

Commit

Permalink
feat(clipboard): should return both text/html and files when clipboar…
Browse files Browse the repository at this point in the history
…d has files
  • Loading branch information
pubuzhixing8 committed Jun 20, 2024
1 parent 2eda267 commit 8479774
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-files-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"slate-angular": minor
---

should return both text/html and files when clipboard has files
7 changes: 4 additions & 3 deletions packages/src/utils/clipboard/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,12 @@ export const createClipboardData = (html: string, elements: Element[], text: str
export const getClipboardData = async (dataTransfer?: DataTransfer): Promise<ClipboardData> => {
let clipboardData = null;
if (dataTransfer) {
let filesData = {};
if (dataTransfer.files.length) {
return { files: Array.from(dataTransfer.files) };
filesData = { ...filesData, files: Array.from(dataTransfer.files) };
}
clipboardData = getDataTransferClipboard(dataTransfer);
return clipboardData;
return { ...clipboardData, ...filesData };
}
if (isClipboardReadSupported()) {
return await getNavigatorClipboard();
Expand All @@ -69,7 +70,7 @@ export const setClipboardData = async (
if (isClipboardWriteSupported()) {
const htmlText = buildHTMLText(wrapper, attach, elements);
// TODO
// maybe fail to write when copy some cell in table
// maybe fail to write when copy some cell in table
return await setNavigatorClipboard(htmlText, elements, text);
}

Expand Down
9 changes: 6 additions & 3 deletions packages/src/utils/clipboard/navigator-clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,26 @@ export const getNavigatorClipboard = async () => {
return new File([blob], 'file', { type: blob.type });
})
);
return {
clipboardData = {
...clipboardData,
files
};
}
if (item.types.includes('text/html')) {
const htmlContent = await blobAsString(await item.getType('text/html'));
const htmlClipboardData = getClipboardFromHTMLText(htmlContent);
if (htmlClipboardData) {
return htmlClipboardData;
clipboardData = { ...clipboardData, ...htmlClipboardData };
return clipboardData;
}
if (htmlContent && htmlContent.trim()) {
clipboardData = { html: htmlContent };
clipboardData = { ...clipboardData, html: htmlContent };
}
}
if (item.types.includes('text/plain')) {
const textContent = await blobAsString(await item.getType('text/plain'));
clipboardData = {
...clipboardData,
text: stripHtml(textContent)
};
}
Expand Down

0 comments on commit 8479774

Please sign in to comment.