Skip to content

Commit

Permalink
Don't call Buffer from renderer (#2825)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyballentine authored Apr 28, 2024
1 parent 6114869 commit 7d4bf2d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/common/safeIpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ export interface InvokeChannels {
void,
[format: string, buffer: Buffer, type?: 'selection' | 'clipboard' | undefined]
>;
'clipboard-writeBuffer-fromString': ChannelInfo<
void,
[format: string, json: string, type?: 'selection' | 'clipboard' | undefined]
>;
'clipboard-readBuffer': ChannelInfo<Buffer, [format: string]>;
'clipboard-readBuffer-toString': ChannelInfo<string, [format: string]>;
'clipboard-availableFormats': ChannelInfo<string[]>;
'clipboard-readHTML': ChannelInfo<string>;
'clipboard-readRTF': ChannelInfo<string>;
Expand Down
6 changes: 6 additions & 0 deletions src/main/gui/main-window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,13 @@ const registerEventHandlerPreSetup = (
ipcMain.handle('clipboard-writeBuffer', (event, format, buffer, type) =>
clipboard.writeBuffer(format, buffer, type)
);
ipcMain.handle('clipboard-writeBuffer-fromString', (event, format, json, type) =>
clipboard.writeBuffer(format, Buffer.from(json), type)
);
ipcMain.handle('clipboard-readBuffer', (event, format) => clipboard.readBuffer(format));
ipcMain.handle('clipboard-readBuffer-toString', (event, format) =>
Buffer.from(clipboard.readBuffer(format)).toString()
);
ipcMain.handle('clipboard-availableFormats', () => clipboard.availableFormats());
ipcMain.handle('clipboard-readHTML', () => clipboard.readHTML());
ipcMain.handle('clipboard-readRTF', () => clipboard.readRTF());
Expand Down
12 changes: 8 additions & 4 deletions src/renderer/helpers/copyAndPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ export const copyToClipboard = (
nodes: nodes.filter((n) => copyIds.has(n.id)),
edges: edges.filter((e) => copyIds.has(e.source) && copyIds.has(e.target)),
};
const copyData = Buffer.from(JSON.stringify(data));
ipcRenderer
.invoke('clipboard-writeBuffer', 'application/chainner.chain', copyData, 'clipboard')
.invoke(
'clipboard-writeBuffer-fromString',
'application/chainner.chain',
JSON.stringify(data),
'clipboard'
)
.catch(log.error);
};

Expand Down Expand Up @@ -61,10 +65,10 @@ export const pasteFromClipboard = async (
if (availableFormats.length === 0) {
try {
const clipboardData = await ipcRenderer.invoke(
'clipboard-readBuffer',
'clipboard-readBuffer-toString',
'application/chainner.chain'
);
const chain = JSON.parse(Buffer.from(clipboardData).toString()) as ClipboardChain;
const chain = JSON.parse(clipboardData) as ClipboardChain;

const duplicationId = createUniqueId();
const deriveId = (oldId: string) => deriveUniqueId(duplicationId + oldId);
Expand Down

0 comments on commit 7d4bf2d

Please sign in to comment.