Skip to main content

最近的文档

¥Recent Documents

概述

¥Overview

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

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

跳转列表:

¥JumpList:

JumpList Recent Files

应用停靠菜单:

¥Application dock menu:

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

要清除最近文档列表,请使用 app.clearRecentDocuments 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.

附加信息

¥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.

当用户单击 JumpList 中的文件时,应用的新实例将启动,并将文件路径添加为命令行参数。

¥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.