X Tutup
Skip to content

Commit dd98fa3

Browse files
docs: Updated "recent documents" fiddle tutorial (electron#29242)
* Port recent-documents fiddle to 12-x-y. * Update recent-documents tutorial. * update for review comments Co-authored-by: Ethan Arrowood <ethan.arrowood@gmail.com>
1 parent 1a30f9f commit dd98fa3

File tree

4 files changed

+51
-31
lines changed

4 files changed

+51
-31
lines changed

docs/fiddles/features/recent-documents/index.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
<html>
33
<head>
44
<meta charset="UTF-8">
5-
<title>Hello World!</title>
5+
<title>Recent Documents</title>
66
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
77
</head>
88
<body>
9-
<h1>Hello World!</h1>
9+
<h1>Recent Documents</h1>
1010
<p>
11-
We are using node <script>document.write(process.versions.node)</script>,
12-
Chrome <script>document.write(process.versions.chrome)</script>,
13-
and Electron <script>document.write(process.versions.electron)</script>.
11+
Right click on the app icon to see recent documents.
12+
You should see `recently-used.md` added to the list of recent files
1413
</p>
1514
</body>
1615
</html>

docs/fiddles/features/recent-documents/main.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ const path = require('path')
55
function createWindow () {
66
const win = new BrowserWindow({
77
width: 800,
8-
height: 600,
9-
webPreferences: {
10-
nodeIntegration: true
11-
}
8+
height: 600
129
})
1310

1411
win.loadFile('index.html')
1512
}
13+
1614
const fileName = 'recently-used.md'
1715
fs.writeFile(fileName, 'Lorem Ipsum', () => {
18-
app.addRecentDocument(path.join(process.cwd(), `${fileName}`))
16+
app.addRecentDocument(path.join(__dirname, fileName))
1917
})
2018

2119
app.whenReady().then(createWindow)

docs/images/recent-documents.png

-8.12 KB
Loading

docs/tutorial/recent-documents.md

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,62 @@ __Application dock menu:__
1313

1414
![macOS Dock Menu][dock-menu-image]
1515

16-
To add a file to recent documents, you need to use the
17-
[app.addRecentDocument][addrecentdocument] API.
18-
1916
## Example
2017

21-
### Add an item to recent documents
22-
23-
Starting with a working application from the
24-
[Quick Start Guide](quick-start.md), add the following lines to the
25-
`main.js` file:
18+
### Managing recent documents
2619

2720
```javascript fiddle='docs/fiddles/features/recent-documents'
28-
const { app } = require('electron')
21+
const { app, BrowserWindow } = require('electron')
22+
const fs = require('fs')
23+
const path = require('path')
24+
25+
function createWindow () {
26+
const win = new BrowserWindow({
27+
width: 800,
28+
height: 600
29+
})
30+
31+
win.loadFile('index.html')
32+
}
33+
34+
const fileName = 'recently-used.md'
35+
fs.writeFile(fileName, 'Lorem Ipsum', () => {
36+
app.addRecentDocument(path.join(__dirname, fileName))
37+
})
2938

30-
app.addRecentDocument('/Users/USERNAME/Desktop/work.type')
39+
app.whenReady().then(createWindow)
40+
41+
app.on('window-all-closed', () => {
42+
app.clearRecentDocuments()
43+
if (process.platform !== 'darwin') {
44+
app.quit()
45+
}
46+
})
47+
48+
app.on('activate', () => {
49+
if (BrowserWindow.getAllWindows().length === 0) {
50+
createWindow()
51+
}
52+
})
3153
```
3254

33-
After launching the Electron application, right click the application icon.
34-
You should see the item you just added. In this guide, the item is a Markdown
35-
file located in the root of the project:
55+
#### Adding a recent document
3656

37-
![Recent document](../images/recent-documents.png)
57+
To add a file to recent documents, use the
58+
[app.addRecentDocument][addrecentdocument] API.
3859

39-
### Clear the list of recent documents
60+
After launching the Electron application, right click the application icon.
61+
In this guide, the item is a Markdown file located in the root of the project.
62+
You should see `recently-used.md` added to the list of recent files:
4063

41-
To clear the list of recent documents, you need to use
42-
[app.clearRecentDocuments][clearrecentdocuments] API in the `main.js` file:
64+
![Recent document](../images/recent-documents.png)
4365

44-
```javascript
45-
const { app } = require('electron')
66+
#### Clearing the list of recent documents
4667

47-
app.clearRecentDocuments()
48-
```
68+
To clear the list of recent documents, use the
69+
[app.clearRecentDocuments][clearrecentdocuments] API.
70+
In this guide, the list of documents is cleared once all windows have been
71+
closed.
4972

5073
## Additional information
5174

0 commit comments

Comments
 (0)
X Tutup