一个小任务
在electron中加入Editor.md,让它看起来像一个真的小型本地编辑器。
quick_start_sample的核心代码分析
首先两个概念:
main process
: main.js
render process
: nameOfPage.html
在render process 中的关键部分:
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
renderer.js中的注释:
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
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
事件监听:
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow)
// Quit when all windows are closed.
app.on('window-all-closed',...
// 处理多次点击桌面图标
app.on('activate', ..
创建初始窗口:
function createWindow () {
// Create the browser window. 用JSON对象设置大小
mainWindow = new BrowserWindow({width: 800, height: 600})
// 用.loadURL加载HTML文件
// 用键'pathname'指定文件路径
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
// 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
})
}
Editor.md 简单样例代码分析
以
\editor.md-master\examples\simple.html
为样例。
Making
重新clone quick-start的时候又出现了安装时的一堆bug,还没发现什么原因,装了electron-download也不行,最后复制上次的node_modules文件夹就好了囧。可能是缺了什么文件?
链接好了文件却在editormd.min.js里提示
Zepto is not defined
html里似乎也没法使用jQuery。Google一下
electron zepto is not defined
SO上给出了完美解答:electron-jquery-is-not-defined
<!-- Insert this line above script imports -->
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
<!-- normal script imports etc -->
<script src="scripts/jquery.min.js"></script>
<script src="scripts/vendor.js"></script>
<!-- Insert this line after script imports -->
<script>if (window.module) module = window.module;</script>
这样子写代码就ok了!
然后还有一些小问题,主要是editor.md的依赖没有放好,就是lib
文件夹。
创建editor时JSON对象中键值path
设定为lib
文件夹,lib
下控制了工具栏图标的显示。
进阶任务1:增加“保存到本地”的功能
【未完成】