docs: added fiddle support for code samples (#26501)

* docs: added fiddle support for code samples in quick start guide and features

* docs: removed excessive fiddle links for not final steps

* docs: added eof newlines to fiddle examples

* docs: reworked fiddle examples to be more self-sufficient

* docs: reworked fiddle examples according to mentions

* docs: changed http to https in the offscreen rendering fiddle

* docs: fix recent documents fiddle to be more consistent
This commit is contained in:
Antonio 2020-11-30 09:48:39 +02:00 committed by GitHub
parent 770e245de5
commit fadd513739
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 787 additions and 29 deletions

View file

@ -96,7 +96,7 @@ color scheme, and update the "Current Theme Source" label to `System`.
To add listeners and handlers, add the following lines to the `renderer.js` file:
```js
```javascript
const { ipcRenderer } = require('electron')
document.getElementById('toggle-dark-mode').addEventListener('click', async () => {
@ -130,7 +130,7 @@ active using the `nativeTheme.shouldUseDarkColors` property, and set the
`themeSource` to the opposite theme.
* Upon receiving `dark-mode:system`, we reset the `themeSource` to `system`.
```js
```javascript
const { app, BrowserWindow, ipcMain, nativeTheme } = require('electron')
function createWindow () {
@ -180,7 +180,7 @@ attribute. The value of `prefers-color-scheme` will follow your
Create a `styles.css` file and add the following lines:
```css
```css fiddle='docs/fiddles/features/macos-dark-mode'
@media (prefers-color-scheme: dark) {
body { background: #333; color: white; }
}

View file

@ -17,7 +17,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), update the `main.js` file with the
following lines:
```js
```javascript fiddle='docs/fiddles/features/keyboard-shortcuts/local'
const { Menu, MenuItem } = require('electron')
const menu = new Menu()
@ -56,7 +56,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), update the `main.js` file with the
following lines:
```js
```javascript fiddle='docs/fiddles/features/keyboard-shortcuts/global'
const { app, globalShortcut } = require('electron')
app.whenReady().then(() => {
@ -101,7 +101,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), update the `main.js` file with the
following lines:
```js
```javascript fiddle='docs/fiddles/features/keyboard-shortcuts/interception-from-main'
const { app, BrowserWindow } = require('electron')
app.whenReady().then(() => {

View file

@ -24,7 +24,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), update the `main.js` file with the
following lines:
```javascript
```javascript fiddle='docs/fiddles/features/macos-dock-menu'
const { app, Menu } = require('electron')
const dockMenu = Menu.buildFromTemplate([

View file

@ -25,7 +25,7 @@ Starting with a working application from the
and add the following lines to the `renderer.js` file:
```js
```javascript
const { ipcRenderer } = require('electron')
document.getElementById('drag').ondragstart = (event) => {
@ -40,7 +40,7 @@ and forward the information to the Main process.
In the Main process(`main.js` file), expand the received event with a path to the file that is
being dragged and an icon:
```javascript
```javascript fiddle='docs/fiddles/features/drag-and-drop'
const { ipcMain } = require('electron')
ipcMain.on('ondragstart', (event, filePath) => {

View file

@ -28,7 +28,7 @@ Assuming you have a working Electron application from the
and add the `renderer.js` file:
```js
```javascript fiddle='docs/fiddles/features/notifications/renderer'
const myNotification = new Notification('Title', {
body: 'Notification from the Renderer process'
})
@ -52,7 +52,7 @@ message that was generated after triggering the `onclick` event:
Starting with a working application from the
[Quick Start Guide](quick-start.md), update the `main.js` file with the following lines:
```js
```javascript fiddle='docs/fiddles/features/notifications/main'
const { Notification } = require('electron')
function showNotification () {

View file

@ -34,7 +34,7 @@ To enable this mode GPU acceleration has to be disabled by calling the
## Usage
``` javascript
```javascript fiddle='docs/fiddles/features/offscreen-rendering'
const { app, BrowserWindow } = require('electron')
app.disableHardwareAcceleration()

View file

@ -34,11 +34,11 @@ let onlineStatusWindow
app.whenReady().then(() => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
onlineStatusWindow.loadURL(`file://${__dirname}/index.html`)
})
```
create the `online-status.html` file and add the following line before the
in the `index.html` file, add the following line before the
closing `</body>` tag:
```html
@ -47,7 +47,7 @@ closing `</body>` tag:
and add the `renderer.js` file:
```javascript
```javascript fiddle='docs/fiddles/features/online-detection/renderer'
const alertOnlineStatus = () => { window.alert(navigator.onLine ? 'online' : 'offline') }
window.addEventListener('online', alertOnlineStatus)
@ -78,7 +78,7 @@ let onlineStatusWindow
app.whenReady().then(() => {
onlineStatusWindow = new BrowserWindow({ width: 0, height: 0, show: false, webPreferences: { nodeIntegration: true } })
onlineStatusWindow.loadURL(`file://${__dirname}/online-status.html`)
onlineStatusWindow.loadURL(`file://${__dirname}/index.html`)
})
ipcMain.on('online-status-changed', (event, status) => {
@ -86,7 +86,7 @@ ipcMain.on('online-status-changed', (event, status) => {
})
```
create the `online-status.html` file and add the following line before the
in the `index.html` file, add the following line before the
closing `</body>` tag:
```html
@ -95,7 +95,7 @@ closing `</body>` tag:
and add the `renderer.js` file:
```javascript
```javascript fiddle='docs/fiddles/features/online-detection/main'
const { ipcRenderer } = require('electron')
const updateOnlineStatus = () => { ipcRenderer.send('online-status-changed', navigator.onLine ? 'online' : 'offline') }

View file

@ -45,7 +45,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), add the following lines to the
`main.js` file:
```javascript
```javascript fiddle='docs/fiddles/features/progress-bar'
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()

View file

@ -28,7 +28,7 @@ If both commands succeeded, you are ready to install Electron.
From a development perspective, an Electron application is essentially a Node.js application. This means that the starting point of your Electron application will be a `package.json` file like in any other Node.js application. A minimal Electron application has the following structure:
```plain
```plaintext
my-electron-app/
├── package.json
├── main.js
@ -53,7 +53,7 @@ The main script specifies the entry point of your Electron application (in our c
The main script may look as follows:
```js
```javascript fiddle='docs/fiddles/quick-start'
const { app, BrowserWindow } = require('electron')
function createWindow () {
@ -66,7 +66,6 @@ function createWindow () {
})
win.loadFile('index.html')
win.webContents.openDevTools()
}
app.whenReady().then(createWindow)
@ -98,7 +97,7 @@ This is the web page you want to display once the application is initialized. Th
The `index.html` page looks as follows:
```html
```html fiddle='docs/fiddles/quick-start'
<!DOCTYPE html>
<html>
<head>
@ -108,9 +107,11 @@ The `index.html` page looks as follows:
</head>
<body style="background: white;">
<h1>Hello World!</h1>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<p>
We are using node <script>document.write(process.versions.node)</script>,
Chrome <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</p>
</body>
</html>
```

View file

@ -24,7 +24,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), add the following lines to the
`main.js` file:
```javascript
```javascript fiddle='docs/fiddles/features/recent-documents'
const { app } = require('electron')
app.addRecentDocument('/Users/USERNAME/Desktop/work.type')

View file

@ -24,7 +24,7 @@ Starting with a working application from the
[Quick Start Guide](quick-start.md), add the following lines to the
`main.js` file:
```javascript
```javascript fiddle='docs/fiddles/features/represented-file'
const { app, BrowserWindow } = require('electron')
app.whenReady().then(() => {