Skip to main content

最近的文档

🌐 Recent Documents

概述

🌐 Overview

Windows 和 macOS 分别通过 JumpList 或 Dock 菜单提供对应用最近打开文档列表的访问。

🌐 Windows and macOS provide access to a list of recent documents opened by the application via JumpList or dock menu, respectively.

跳转列表:

JumpList Recent Files

应用停靠栏菜单:

macOS Dock Menu

示例

🌐 Example

管理最近的文档

🌐 Managing recent documents

const { app, BrowserWindow } = require('electron/main')
const fs = require('node:fs')
const path = require('node:path')

function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600
})

win.loadFile('index.html')
}

const fileName = 'recently-used.md'
fs.writeFile(fileName, 'Lorem Ipsum', () => {
app.addRecentDocument(path.join(__dirname, fileName))
})

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
app.clearRecentDocuments()
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})

添加最近的文档

🌐 Adding a recent document

要将文件添加到最近使用的文档中,请使用 app.addRecentDocument API。

🌐 To add a file to recent documents, use the app.addRecentDocument API.

启动 Electron 应用后,右键点击应用图标。 在本指南中,该项目是位于项目根目录下的 Markdown 文件。 你应该会在最近使用的文件列表中看到 recently-used.md 被添加进去:

🌐 After launching the Electron application, right click the application icon. In this guide, the item is a Markdown file located in the root of the project. You should see recently-used.md added to the list of recent files:

Recent document

清除最近文档列表

🌐 Clearing the list of recent documents

要清除最近文档列表,请使用 应用.清除最近文档 API。在本指南中,文档列表将在所有窗口关闭后清除。

🌐 To clear the list of recent documents, use the app.clearRecentDocuments API. In this guide, the list of documents is cleared once all windows have been closed.

访问最近文档列表

🌐 Accessing the list of recent documents

要访问最近文档的列表,请使用 app.getRecentDocuments API。

🌐 To access the list of recent documents, use the app.getRecentDocuments API.

附加信息

🌐 Additional information

Windows 注意

🌐 Windows Notes

要在 Windows 上使用此功能,你的应用必须注册为该文档文件类型的处理程序,否则即使你已添加文件,它也不会出现在 JumpList 中。你可以在 应用注册 上找到有关注册应用的所有信息。

🌐 To use this feature on Windows, your application has to be registered as a handler of the file type of the document, otherwise the file won't appear in JumpList even after you have added it. You can find everything on registering your application in Application Registration.

当用户从跳转列表中点击一个文件时,将启动你的应用的新实例,并将该文件的路径作为命令行参数添加。

🌐 When a user clicks a file from the JumpList, a new instance of your application will be started with the path of the file added as a command line argument.

macOS 注意

🌐 macOS Notes

将最近文档列表添加到应用菜单

🌐 Add the Recent Documents list to the application menu

你可以通过将以下代码片段添加到你的菜单模板中,来添加菜单项以访问和清除最近的文档:

🌐 You can add menu items to access and clear recent documents by adding the following code snippet to your menu template:

{
"submenu":[
{
"label":"Open Recent",
"role":"recentdocuments",
"submenu":[
{
"label":"Clear Recent",
"role":"clearrecentdocuments"
}
]
}
]
}

确保应用菜单是在 'ready' 事件之后添加的,而不是之前,否则菜单项将被禁用:

🌐 Make sure the application menu is added after the 'ready' event and not before, or the menu item will be disabled:

const { app, Menu } = require('electron')

const template = [
// Menu template here
]
const menu = Menu.buildFromTemplate(template)

app.whenReady().then(() => {
Menu.setApplicationMenu(menu)
})

macOS Recent Documents menu item

当从最近文档菜单请求一个文件时,app 模块的 open-file 事件将会为该文件触发。

🌐 When a file is requested from the recent documents menu, the open-file event of app module will be emitted for it.