Browse Source

docs: update clipboard fiddles (#36961)

Co-authored-by: David Sanders <[email protected]>

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: David Sanders <[email protected]>
trop[bot] 2 years ago
parent
commit
b6eef90604

+ 1 - 1
docs/api/clipboard.md

@@ -2,7 +2,7 @@
 
 > Perform copy and paste operations on the system clipboard.
 
-Process: [Main](../glossary.md#main-process), [Renderer](../glossary.md#renderer-process)
+Process: [Main](../glossary.md#main-process), [Renderer](../glossary.md#renderer-process) (non-sandboxed only)
 
 On Linux, there is also a `selection` clipboard. To manipulate it
 you need to pass `selection` to each method:

+ 0 - 0
docs/fiddles/system/clipboard/.keep


+ 3 - 4
docs/fiddles/system/clipboard/copy/index.html

@@ -2,12 +2,13 @@
 <html>
   <head>
     <meta charset="UTF-8">
+    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
   </head>
   <body>
     <div>
       <div>
         <h1>Clipboard copy</h1>
-        <i>Supports: Win, macOS, Linux <span>|</span> Process: Both</i>
+        <i>Supports: Win, macOS, Linux <span>|</span> Process: Main, Renderer (non-sandboxed only)</i>
         <div>
           <div>
             <button id="copy-to">Copy</button>
@@ -17,8 +18,6 @@
         </div>
       </div>
     </div>
+    <script src="./renderer.js"></script>
   </body>
-  <script>
-    require('./renderer.js')
-  </script>
 </html>

+ 15 - 2
docs/fiddles/system/clipboard/copy/main.js

@@ -1,4 +1,5 @@
-const { app, BrowserWindow } = require('electron')
+const { app, BrowserWindow, ipcMain, clipboard } = require('electron')
+const path = require('path')
 
 let mainWindow = null
 
@@ -8,7 +9,7 @@ function createWindow () {
     height: 400,
     title: 'Clipboard copy',
     webPreferences: {
-      nodeIntegration: true
+      preload: path.join(__dirname, 'preload.js')
     }
   }
 
@@ -20,6 +21,18 @@ function createWindow () {
   })
 }
 
+ipcMain.handle('clipboard:writeText', (event, text) => {
+  clipboard.writeText(text)
+})
+
 app.whenReady().then(() => {
   createWindow()
+  
+  app.on('activate', function () {
+    if (BrowserWindow.getAllWindows().length === 0) createWindow()
+  })
+})
+
+app.on('window-all-closed', function () {
+  if (process.platform !== 'darwin') app.quit()
 })

+ 5 - 0
docs/fiddles/system/clipboard/copy/preload.js

@@ -0,0 +1,5 @@
+const { contextBridge, ipcRenderer } = require('electron')
+
+contextBridge.exposeInMainWorld('clipboard', {
+  writeText: (text) => ipcRenderer.invoke('clipboard:writeText', text)
+})

+ 0 - 2
docs/fiddles/system/clipboard/copy/renderer.js

@@ -1,5 +1,3 @@
-const { clipboard } = require('electron')
-
 const copyBtn = document.getElementById('copy-to')
 const copyInput = document.getElementById('copy-to-input')
 

+ 3 - 4
docs/fiddles/system/clipboard/paste/index.html

@@ -2,12 +2,13 @@
 <html>
   <head>
     <meta charset="UTF-8">
+    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
   </head>
   <body>
     <div>
       <div>
         <h1>Clipboard paste</h1>
-        <i>Supports: Win, macOS, Linux <span>|</span> Process: Both</i>
+        <i>Supports: Win, macOS, Linux <span>|</span> Process: Main, Renderer (non-sandboxed only)</i>
         <div>
           <div>
             <button id="paste-to">Paste</button>
@@ -17,8 +18,6 @@
         </div>
       </div>
     </div>
+    <script src="./renderer.js"></script>
   </body>
-  <script>
-    require('./renderer.js')
-  </script>
 </html>

+ 19 - 2
docs/fiddles/system/clipboard/paste/main.js

@@ -1,4 +1,5 @@
-const { app, BrowserWindow } = require('electron')
+const { app, BrowserWindow, ipcMain, clipboard } = require('electron')
+const path = require('path')
 
 let mainWindow = null
 
@@ -8,7 +9,7 @@ function createWindow () {
     height: 400,
     title: 'Clipboard paste',
     webPreferences: {
-      nodeIntegration: true
+      preload: path.join(__dirname, 'preload.js')
     }
   }
 
@@ -20,6 +21,22 @@ function createWindow () {
   })
 }
 
+ipcMain.handle('clipboard:readText', () => {
+  return clipboard.readText()
+})
+
+ipcMain.handle('clipboard:writeText', (event, text) => {
+  clipboard.writeText(text)
+})
+
 app.whenReady().then(() => {
   createWindow()
+  
+  app.on('activate', function () {
+    if (BrowserWindow.getAllWindows().length === 0) createWindow()
+  })
+})
+
+app.on('window-all-closed', function () {
+  if (process.platform !== 'darwin') app.quit()
 })

+ 6 - 0
docs/fiddles/system/clipboard/paste/preload.js

@@ -0,0 +1,6 @@
+const { contextBridge, ipcRenderer } = require('electron')
+
+contextBridge.exposeInMainWorld('clipboard', {
+  readText: () => ipcRenderer.invoke('clipboard:readText'),
+  writeText: (text) => ipcRenderer.invoke('clipboard:writeText', text)
+})

+ 3 - 5
docs/fiddles/system/clipboard/paste/renderer.js

@@ -1,9 +1,7 @@
-const { clipboard } = require('electron')
-
 const pasteBtn = document.getElementById('paste-to')
 
-pasteBtn.addEventListener('click', () => {
-  clipboard.writeText('What a demo!')
-  const message = `Clipboard contents: ${clipboard.readText()}`
+pasteBtn.addEventListener('click', async () => {
+  await clipboard.writeText('What a demo!')
+  const message = `Clipboard contents: ${await clipboard.readText()}`
   document.getElementById('paste-from').innerHTML = message
 })