核心构建逻辑与Atom编辑器工作逻辑分离(图片粘贴)
This commit is contained in:
parent
cef22e2ff6
commit
82f217af29
|
|
@ -132,5 +132,12 @@ module.exports = {
|
|||
fs.unlinkSync(path2);
|
||||
}
|
||||
}
|
||||
},
|
||||
//创建文件夹
|
||||
createFolder: function (path, callback) {
|
||||
if (!fs.existsSync(path)) {
|
||||
fs.mkdirSync(path, 0o777);
|
||||
}
|
||||
callback && callback();
|
||||
}
|
||||
};
|
||||
|
|
@ -7,15 +7,15 @@ let fs = require('fs');
|
|||
|
||||
module.exports = {
|
||||
//创建文库id
|
||||
createWikiId: function (path) {
|
||||
path = path.replace(/\\/g, '/');
|
||||
createWikiId: function (root) {
|
||||
root = root.replace(/\\/g, '/');
|
||||
//累加地址字符串每个字符Unicode值与此字符序号的乘积
|
||||
let code = 0;
|
||||
for (let j = 0; j < path.length; j++) {
|
||||
code += path.charCodeAt(j) * j;
|
||||
for (let j = 0; j < root.length; j++) {
|
||||
code += root.charCodeAt(j) * j;
|
||||
}
|
||||
//再与地址字符串长度拼合
|
||||
code = parseInt(path.length + '' + code);
|
||||
code = parseInt(root.length + '' + code);
|
||||
return code;
|
||||
},
|
||||
//判断一个文件夹是否为amWiki文库项目
|
||||
|
|
|
|||
25
lib/main.js
25
lib/main.js
|
|
@ -88,26 +88,31 @@ module.exports = {
|
|||
//命令:在当前文档上抓取h2、h3为页内目录
|
||||
'amWiki:catalogue': function () {
|
||||
let [isOnEdit, editor, editPath] = that._getEditorPath();
|
||||
if (!isOnEdit) {
|
||||
alert('只有当你打开library文件夹下的文档时,才能提取页内目录!');
|
||||
} else {
|
||||
if (isOnEdit) {
|
||||
pageCatalogue.make(editor);
|
||||
that._checkAddWiki(editPath);
|
||||
} else {
|
||||
alert('只有当你打开 library 文件夹下的 .md 文档时,才能提取页内目录!');
|
||||
}
|
||||
},
|
||||
//命令:手动更新当前文库导航
|
||||
'amWiki:makeNav': function () {
|
||||
let [isOnEdit, editor, editPath] = that._getEditorPath();
|
||||
if (!isOnEdit) {
|
||||
alert('只有当你打开library文件夹下的文档时,才能手动更新导航文件!');
|
||||
} else {
|
||||
if (isOnEdit || (typeof editPath !== 'undefined' && mngWiki.isAmWiki(editPath))) {
|
||||
makeNav.refresh(editPath);
|
||||
that._checkAddWiki(editPath);
|
||||
} else {
|
||||
alert('只有当你打开一个 amWiki 文库时,才能手动更新导航文件!');
|
||||
}
|
||||
},
|
||||
//命令:粘帖截图
|
||||
'amWiki:paste': function () {
|
||||
pasteImg.paster();
|
||||
let [isOnEdit, editor, editPath] = that._getEditorPath();
|
||||
if (isOnEdit) {
|
||||
pasteImg.paster(editor, editPath);
|
||||
} else {
|
||||
alert('只有当你打开 library 文件夹下的 .md 文档时,才能快捷粘贴图片!');
|
||||
}
|
||||
},
|
||||
//命令:启动node静态服务器
|
||||
'amWiki:runServer': function () {
|
||||
|
|
@ -168,14 +173,14 @@ module.exports = {
|
|||
}
|
||||
grammar = editor.getGrammar();
|
||||
if (!grammar) {
|
||||
return [false];
|
||||
return [false, editor];
|
||||
}
|
||||
let ePath = editor.getPath();
|
||||
if (editor.getPath().substr(-3) !== '.md') {
|
||||
return [false];
|
||||
return [false, editor, ePath];
|
||||
}
|
||||
if (ePath.indexOf('library') < 0) {
|
||||
return [false];
|
||||
return [false, editor, ePath];
|
||||
}
|
||||
return [true, editor, ePath];
|
||||
},
|
||||
|
|
|
|||
|
|
@ -4,51 +4,38 @@
|
|||
*/
|
||||
|
||||
let environment = require('atom'),
|
||||
File = environment.File,
|
||||
Directory = environment.Directory;
|
||||
File = environment.File;
|
||||
let fs = require("fs");
|
||||
let clipboard = require('clipboard');
|
||||
let crypto = require("crypto");
|
||||
let crypto = require('crypto');
|
||||
let mngFolder = require('../build/manage-folder');
|
||||
|
||||
module.exports = {
|
||||
paster: function () {
|
||||
let editor = atom.workspace.getActiveTextEditor();
|
||||
//状态验证,当编辑md文档时才允许操作
|
||||
let grammar, img;
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
grammar = editor.getGrammar();
|
||||
if (!grammar) {
|
||||
return;
|
||||
}
|
||||
if (editor.getPath().substr(-3) !== '.md') {
|
||||
return;
|
||||
}
|
||||
img = clipboard.readImage();
|
||||
paster: function (editor, editPath) {
|
||||
let img = clipboard.readImage();
|
||||
if (img.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
let paths = this.getPaths(editor.getPath());
|
||||
if (!paths) {
|
||||
return;
|
||||
}
|
||||
let imgbuffer = img.toPng();
|
||||
//文件保存
|
||||
let md5 = crypto.createHash('md5');
|
||||
let imgbuffer = img.toPng();
|
||||
md5.update(imgbuffer);
|
||||
let filename = "" + (paths.mdFile.getBaseName().replace(/\.\w+$/, '').replace(/\s+/g, '').split(/[-_]/)[0]) +
|
||||
let {mdFile, assetsDirPath, createDirPath, insertPath, writePath} = this._getPaths(editPath);
|
||||
if (!mdFile) {
|
||||
return;
|
||||
}
|
||||
let filename = "" + (mdFile.getBaseName().replace(/\.\w+$/, '').replace(/\s+/g, '').split(/[-_]/)[0]) +
|
||||
"-" + (md5.digest('hex').slice(0, 8)) + ".png";
|
||||
this.createDirectory(paths.creatDirPath, function () {
|
||||
fs.writeFile(paths.writePath + filename, imgbuffer, 'binary', function () {
|
||||
editor.insertText("", editor);
|
||||
mngFolder.createFolder(createDirPath, function () {
|
||||
fs.writeFile(writePath + filename, imgbuffer, 'binary', function () {
|
||||
editor.insertText("", editor);
|
||||
});
|
||||
});
|
||||
},
|
||||
getPaths: function (editorPath) {
|
||||
_getPaths: function (editorPath) {
|
||||
//路径计算
|
||||
let assetsDirPath, //项目assets文件夹地址
|
||||
creatDirPath, //本次创建图片地址
|
||||
createDirPath, //本次创建图片地址
|
||||
insertPath, //插入文档的图片引用路径
|
||||
writePath; //图片输出路径
|
||||
let mdFile = new File(editorPath);
|
||||
|
|
@ -56,7 +43,7 @@ module.exports = {
|
|||
//library直接子级
|
||||
if (mdFile.getParent().getBaseName() === 'library') {
|
||||
assetsDirPath = mdFile.getParent().getParent().getPath() + "/assets";
|
||||
creatDirPath = assetsDirPath + '/';
|
||||
createDirPath = assetsDirPath + '/';
|
||||
writePath = assetsDirPath + '/';
|
||||
insertPath = '';
|
||||
}
|
||||
|
|
@ -64,7 +51,7 @@ module.exports = {
|
|||
else if (mdFile.getParent().getParent().getBaseName() === 'library') {
|
||||
parentDir = mdFile.getParent();
|
||||
assetsDirPath = parentDir.getParent().getParent().getPath() + "/assets";
|
||||
creatDirPath = assetsDirPath + '/' + parentDir.getBaseName().split(/[-_]/)[0];
|
||||
createDirPath = assetsDirPath + '/' + parentDir.getBaseName().split(/[-_]/)[0];
|
||||
writePath = assetsDirPath + '/' + parentDir.getBaseName().split(/[-_]/)[0] + '/';
|
||||
insertPath = parentDir.getBaseName().split(/[-_]/)[0] + '/';
|
||||
}
|
||||
|
|
@ -72,36 +59,20 @@ module.exports = {
|
|||
else if (mdFile.getParent().getParent().getParent().getBaseName() === 'library') {
|
||||
parentDir = mdFile.getParent().getParent();
|
||||
assetsDirPath = parentDir.getParent().getParent().getPath() + "/assets";
|
||||
creatDirPath = assetsDirPath + '/' + parentDir.getBaseName().split(/[-_]/)[0];
|
||||
createDirPath = assetsDirPath + '/' + parentDir.getBaseName().split(/[-_]/)[0];
|
||||
writePath = assetsDirPath + '/' + parentDir.getBaseName().split(/[-_]/)[0] + '/';
|
||||
insertPath = parentDir.getBaseName().split(/[-_]/)[0] + '/';
|
||||
}
|
||||
//其他目录
|
||||
else {
|
||||
return null;
|
||||
return {};
|
||||
}
|
||||
return {
|
||||
mdFile: mdFile,
|
||||
assetsDirPath: assetsDirPath,
|
||||
creatDirPath: creatDirPath,
|
||||
createDirPath: createDirPath,
|
||||
insertPath: insertPath,
|
||||
writePath: writePath
|
||||
}
|
||||
},
|
||||
createDirectory: function (dirPath, callback) {
|
||||
let that = this;
|
||||
let assetsDir;
|
||||
assetsDir = new Directory(dirPath);
|
||||
assetsDir.exists().then(function (existed) {
|
||||
if (!existed) {
|
||||
assetsDir.create().then(function (created) {
|
||||
if (created) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue