如何在浏览器窗口(电子)中打开降价文件
How do I open a markdown file in a BrowserWindow (Electron)?
我正在尝试扩展一个电子项目。我有一个新的浏览器窗口(基本上是浏览器中的新选项卡),它应该包含文档。文档是用 markdown 编写的,所以我想知道如何在这个浏览器窗口中打开我的 markdown 文件......
基本上,我只需要一种方法将运行时的markdown文件转换为网页。
你需要节点模块 fs 来打开文件,并且有一个名为 mark 的 js 库 - 在 npm 中查找它。它呈现降价。
更新 - 这是一个最小的电子应用程序示例,在电子 0.37.8 上测试。
//start - package.json:
{
"name": "mini-md-example",
"version": "0.1.0",
"description": "A small Electron application to open/display a markdown file",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"devDependencies": {
"electron-prebuilt": "^0.37.7",
"marked": "^0.3.5"
}
}
//:end - package.json
//start - main.js:
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow
const fs = require('fs');
var dialog = require('dialog')
var path = require('path')
var defaultMenu = require('./def-menu-main')
var Menu = require('menu')
const {ipcMain} = electron;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 999, height: 800})
// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`)
// Open the DevTools.
mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})
var OpenFile = function() {
dialog.showOpenDialog(mainWindow, {
filters: [{name: 'Markdown', extensions: ['md', 'markdown']}],
properties: ['openFile']
}, function(paths) {
if (!paths) return false;
var fPath = paths[0];
var fName = path.basename(fPath);
var fData = fs.readFileSync(fPath, 'utf8');
mainWindow.webContents.send('file-open', fPath, fName, fData);
})
}
var SendEvent = function(name) {
return function() {mainWindow.webContents.send(name);};
};
// Get template for default menu
var menu = defaultMenu()
// Add my very own custom FILE menu
menu.splice(0, 0, {
label: 'File',
submenu: [
{
label: 'Open',
accelerator: "CmdOCtrl+O",
click: OpenFile
},
]
})
// Set top-level application menu, using modified template
Menu.setApplicationMenu(Menu.buildFromTemplate(menu));
//:end - main.js
//start - index.html:
<!DOCTYPE html>
<html>
<body>
<div id="content"></div>
<script>
var marked = require('marked')
var ipc = require('electron').ipcRenderer
ipc.on('file-open', function(event, fPath, filename, filedata)
{
document.getElementById('content').innerHTML = marked(filedata) ;
})
</script>
</body>
</html>
//:end - index.html
//start - def-menu-main.js:
var electron = require('electron') // this should work if you're in the electron environment
//var app = electron.remote.app
// original app var calls remote as if this is used in a renderer, but for me menus are a main app thing
var app = electron.app
var shell = electron.shell
module.exports = function() {
var template = [
{
label: 'View',
submenu: [
{
label: 'Reload',
accelerator: 'CmdOrCtrl+R',
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.reload();
}
},
{
label: 'Toggle Full Screen',
accelerator: (function() {
if (process.platform === 'darwin')
return 'Ctrl+Command+F';
else
return 'F11';
})(),
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.setFullScreen(!focusedWindow.isFullScreen());
}
},
{
label: 'Toggle Developer Tools',
accelerator: (function() {
if (process.platform === 'darwin')
return 'Alt+Command+I';
else
return 'Ctrl+Shift+I';
})(),
click: function(item, focusedWindow) {
if (focusedWindow)
focusedWindow.toggleDevTools();
}
},
]
},
{
label: 'Window',
role: 'window',
submenu: [
{
label: 'Minimize',
accelerator: 'CmdOrCtrl+M',
role: 'minimize'
},
{
label: 'Close',
accelerator: 'CmdOrCtrl+W',
role: 'close'
},
]
},
{
label: 'Help',
role: 'help',
submenu: [
{
label: 'Learn More',
click: function() { shell.openExternal('http://electron.atom.io') }
},
]
},
];
if (process.platform === 'darwin') {
var name = app.getName();
template.unshift({
label: name,
submenu: [
{
label: 'About ' + name,
role: 'about'
},
{
type: 'separator'
},
{
label: 'Services',
role: 'services',
submenu: []
},
{
type: 'separator'
},
{
label: 'Hide ' + name,
accelerator: 'Command+H',
role: 'hide'
},
{
label: 'Hide Others',
accelerator: 'Command+Shift+H',
role: 'hideothers'
},
{
label: 'Show All',
role: 'unhide'
},
{
type: 'separator'
},
{
label: 'Quit',
accelerator: 'Command+Q',
click: function() { app.quit(); }
},
]
});
var windowMenu = template.find(function(m) { return m.role === 'window' })
if (windowMenu) {
windowMenu.submenu.push(
{
type: 'separator'
},
{
label: 'Bring All to Front',
role: 'front'
}
);
}
}
return template;
}
//:end - def-menu-main.js
相关文章:
- 谷歌电子表格,脚本,备份文件,子文件夹
- 如何使用谷歌应用程序脚本从电子邮件中提取内联文件/图像
- 如何在不干扰电子邮件通知的情况下将眼镜保存在文本文件中
- 如何检查当前用户电子邮件是否已在客户端js文件中验证
- 从用户id和电子邮件的.csv文件在facebook上发送好友请求
- 创建到电子表格创建错误中的文件的链接的脚本“;无法转换“”;
- 是否可以使用 javascript 读取文本或电子表格文件
- 如何在java中通过电子邮件发送渲染的html文件
- 不能在电子中包含js文件
- 下载并存储在电子应用程序中的文件
- 使用 jQuery 上传电子邮件文件
- Phonegap插件:检索用户配置文件,电子邮件和Gmail
- 使用 Outlook 通过电子邮件发送临时文件
- 客户端 JavaScript CSV 文件生成和电子邮件附件
- 如何在Magento电子商务网站版本1.3.2.4中包含带有脚本的自定义js文件
- 使用ASP发送电子邮件,但首先使用外部JS文件验证表单字段
- 通过解析云模块Sendgrid通过电子邮件发送文件
- 如何使用我的表单将附件文件包含到电子邮件中
- 用户单击提交后,页面将转到php文件.它如何保留在HTML文件中并将表单信息提交到电子邮件中
- 如何在web应用程序上查看电子绘图文件