当前文档空间路径上传图片绝对路径计算bugfix,支持数学表达式输入快捷键

This commit is contained in:
xiaoqi.cxq 2022-11-11 17:01:41 +08:00
parent d3193e1739
commit 808891e47c
8 changed files with 81 additions and 28 deletions

View File

@ -21,10 +21,10 @@
<b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">CouchDB 数据库</a>同步 <b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">CouchDB 数据库</a>同步
</span> </span>
<span v-else-if="currentWorkspace.providerId === 'githubWorkspace'"> <span v-else-if="currentWorkspace.providerId === 'githubWorkspace'">
<b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">GitHub repo</a> 同步 <b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">GitHub 仓库</a> 同步
</span> </span>
<span v-else-if="currentWorkspace.providerId === 'giteeWorkspace'"> <span v-else-if="currentWorkspace.providerId === 'giteeWorkspace'">
<b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">Gitee repo</a> 同步 <b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">Gitee 仓库</a> 同步
</span> </span>
<span v-else-if="currentWorkspace.providerId === 'gitlabWorkspace'"> <span v-else-if="currentWorkspace.providerId === 'gitlabWorkspace'">
<b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">GitLab 项目</a>同步 <b>{{currentWorkspace.name}}</b> <a :href="workspaceLocationUrl" target="_blank">GitLab 项目</a>同步

View File

@ -36,9 +36,11 @@ export default modalTemplate({
}, },
methods: { methods: {
resolve() { resolve() {
const path = this.path && this.path.replace(/^\//, ''); if (!this.path) {
this.setError('path');
}
this.config.resolve({ this.config.resolve({
path: path || '/imgs/{YYYY}-{MM}-{DD}', path: this.path || '/imgs/{YYYY}-{MM}-{DD}',
}); });
}, },
}, },

View File

@ -38,6 +38,7 @@ shortcuts:
mod+shift+s: strikethrough mod+shift+s: strikethrough
mod+shift+t: table mod+shift+t: table
mod+shift+u: ulist mod+shift+u: ulist
mod+shift+f: inlineformula
'= = > space': '= = > space':
method: expand method: expand
params: params:

View File

@ -40,7 +40,9 @@ var defaultsStrings = {
undo: "Undo - Ctrl/Cmd+Z", undo: "Undo - Ctrl/Cmd+Z",
redo: "Redo - Ctrl/Cmd+Y", redo: "Redo - Ctrl/Cmd+Y",
help: "Markdown Editing Help" help: "Markdown Editing Help",
formulaexample: "这里输入Latex表达式",
}; };
// options, if given, can have the following properties: // options, if given, can have the following properties:
@ -465,6 +467,7 @@ function UIManager(input, commandManager) {
buttons.bold = bindCommand("doBold"); buttons.bold = bindCommand("doBold");
buttons.italic = bindCommand("doItalic"); buttons.italic = bindCommand("doItalic");
buttons.strikethrough = bindCommand("doStrikethrough"); buttons.strikethrough = bindCommand("doStrikethrough");
buttons.inlineformula = bindCommand("doInlinkeFormula");
buttons.imageUploading = bindCommand("doImageUploading"); buttons.imageUploading = bindCommand("doImageUploading");
buttons.link = bindCommand(function (chunk, postProcessing) { buttons.link = bindCommand(function (chunk, postProcessing) {
return this.doLinkOrImage(chunk, postProcessing, false); return this.doLinkOrImage(chunk, postProcessing, false);
@ -618,6 +621,49 @@ commandProto.doStrikethrough = function (chunk, postProcessing) {
return; return;
}; };
commandProto.doInlinkeFormula = function (chunk, postProcessing) {
// Get rid of whitespace and fixup newlines.
chunk.trimWhitespace();
chunk.selection = chunk.selection.replace(/\n{2,}/g, "\n");
// Look for stars before and after. Is the chunk already marked up?
// note that these regex matches cannot fail
var starsBefore = /(\$*$)/.exec(chunk.before)[0];
var starsAfter = /(^\$*)/.exec(chunk.after)[0];
var prevStars = Math.min(starsBefore.length, starsAfter.length);
var nStars = 2;
// Remove stars if we have to since the button acts as a toggle.
if ((prevStars >= nStars) && (prevStars != 2 || nStars != 1)) {
chunk.before = chunk.before.replace(re("[\$]{" + nStars + "}$", ""), "");
chunk.after = chunk.after.replace(re("^[\$]{" + nStars + "}", ""), "");
} else if (!chunk.selection && starsAfter) {
// It's not really clear why this code is necessary. It just moves
// some arbitrary stuff around.
chunk.after = chunk.after.replace(/^(\$*)/, "");
chunk.before = chunk.before.replace(/(\s?)$/, "");
var whitespace = re.$1;
chunk.before = chunk.before + starsAfter + whitespace;
} else {
// In most cases, if you don't have any selected text and click the button
// you'll get a selected, marked up region with the default text inserted.
if (!chunk.selection && !starsAfter) {
chunk.selection = this.getString("formulaexample");
}
// Add the true markup.
var markup = "$"; // shouldn't the test be = ?
chunk.before = chunk.before + markup;
chunk.after = markup + chunk.after;
}
return;
};
commandProto.doImageUploading = function (chunk, postProcessing) { commandProto.doImageUploading = function (chunk, postProcessing) {
var enteredCallback = function (imgId) { var enteredCallback = function (imgId) {
if (imgId !== null) { if (imgId !== null) {

View File

@ -46,16 +46,10 @@ class SectionDesc {
const pathUrlMap = Object.create(null); const pathUrlMap = Object.create(null);
const getCurrAbsolutePath = () => {
const fileId = store.getters['file/current'].id;
const fileSyncData = store.getters['data/syncDataByItemId'][fileId] || { id: '' };
const fileAbsolutePath = `${store.getters['workspace/currentWorkspace'].path || ''}${fileSyncData.id}`;
return fileAbsolutePath.substring(0, fileAbsolutePath.lastIndexOf('/'));
};
const getImgUrl = async (uri) => { const getImgUrl = async (uri) => {
if (uri.indexOf('http://') !== 0 && uri.indexOf('https://') !== 0) { if (uri.indexOf('http://') !== 0 && uri.indexOf('https://') !== 0) {
const absoluteImgPath = utils.getAbsoluteFilePath(getCurrAbsolutePath(), uri); const currDirNode = store.getters['explorer/selectedNodeFolder'];
const absoluteImgPath = utils.getAbsoluteFilePath(currDirNode, uri);
if (pathUrlMap[absoluteImgPath]) { if (pathUrlMap[absoluteImgPath]) {
return pathUrlMap[absoluteImgPath]; return pathUrlMap[absoluteImgPath];
} }

View File

@ -7,14 +7,7 @@ import giteaHelper from '../services/providers/helpers/giteaHelper';
import githubHelper from '../services/providers/helpers/githubHelper'; import githubHelper from '../services/providers/helpers/githubHelper';
import customHelper from '../services/providers/helpers/customHelper'; import customHelper from '../services/providers/helpers/customHelper';
function getCurrAbsolutePath() { const getImagePath = (confPath, imgType) => {
const fileId = store.getters['file/current'].id;
const fileSyncData = store.getters['data/syncDataByItemId'][fileId] || { id: '' };
const fileAbsolutePath = `${store.getters['workspace/currentWorkspace'].path || ''}${fileSyncData.id}`;
return fileAbsolutePath.substring(0, fileAbsolutePath.lastIndexOf('/'));
}
function getImagePath(confPath, imgType) {
const time = new Date(); const time = new Date();
const date = time.getDate(); const date = time.getDate();
const month = time.getMonth() + 1; const month = time.getMonth() + 1;
@ -22,7 +15,7 @@ function getImagePath(confPath, imgType) {
const path = confPath.replace('{YYYY}', year).replace('{MM}', `0${month}`.slice(-2)) const path = confPath.replace('{YYYY}', year).replace('{MM}', `0${month}`.slice(-2))
.replace('{DD}', `0${date}`.slice(-2)).replace('{MDNAME}', store.getters['file/current'].name); .replace('{DD}', `0${date}`.slice(-2)).replace('{MDNAME}', store.getters['file/current'].name);
return `${path}${path.endsWith('/') ? '' : '/'}${utils.uid()}.${imgType.split('/')[1]}`; return `${path}${path.endsWith('/') ? '' : '/'}${utils.uid()}.${imgType.split('/')[1]}`;
} };
export default { export default {
// 上传图片 返回图片链接 // 上传图片 返回图片链接
@ -38,13 +31,14 @@ export default {
const path = getImagePath(currStorage.sub, imgFile.type); const path = getImagePath(currStorage.sub, imgFile.type);
// 保存到indexeddb // 保存到indexeddb
const base64 = await utils.encodeFiletoBase64(imgFile); const base64 = await utils.encodeFiletoBase64(imgFile);
const absolutePath = utils.getAbsoluteFilePath(getCurrAbsolutePath(), path); const currDirNode = store.getters['explorer/selectedNodeFolder'];
const absolutePath = utils.getAbsoluteFilePath(currDirNode, path);
await localDbSvc.saveImg({ await localDbSvc.saveImg({
id: md5(absolutePath), id: md5(absolutePath),
path: absolutePath, path: absolutePath,
content: base64, content: base64,
}); });
return { url: path.replace(' ', '%20') }; return { url: path.replaceAll(' ', '%20') };
} }
if (!currStorage.provider) { if (!currStorage.provider) {
return { error: '暂无已选择的图床!' }; return { error: '暂无已选择的图床!' };

View File

@ -32,7 +32,9 @@ const methods = {
ulist: pagedownHandler('ulist'), ulist: pagedownHandler('ulist'),
clist: pagedownHandler('clist'), clist: pagedownHandler('clist'),
heading: pagedownHandler('heading'), heading: pagedownHandler('heading'),
inline: pagedownHandler('heading'),
hr: pagedownHandler('hr'), hr: pagedownHandler('hr'),
inlineformula: pagedownHandler('inlineformula'),
sync() { sync() {
if (syncSvc.isSyncPossible()) { if (syncSvc.isSyncPossible()) {
syncSvc.requestSync(); syncSvc.requestSync();

View File

@ -383,21 +383,35 @@ export default {
elt.parentNode.removeChild(elt); elt.parentNode.removeChild(elt);
}); });
}, },
getAbsoluteDir(currDirNode) {
if (!currDirNode) {
return '';
}
let path = currDirNode.item.name;
if (currDirNode.parentNode) {
const parentPath = this.getAbsoluteDir(currDirNode.parentNode);
if (parentPath) {
path = `${parentPath}/${path}`;
}
}
return path || '';
},
// 根据当前绝对路径 与 文件路径计算出文件绝对路径 // 根据当前绝对路径 与 文件路径计算出文件绝对路径
getAbsoluteFilePath(currAbsolutePath, filePath) { getAbsoluteFilePath(currDirNode, filePath) {
const currAbsolutePath = this.getAbsoluteDir(currDirNode);
// "/"开头说明已经是绝对路径 // "/"开头说明已经是绝对路径
if (filePath.indexOf('/') === 0) { if (filePath.indexOf('/') === 0) {
return this.encodeUrlPath(filePath); return filePath.replaceAll(' ', '%20');
} }
let path = filePath; let path = filePath;
// 相对上级路径 // 相对上级路径
if (path.indexOf('../') === 0) { if (path.indexOf('../') === 0) {
return this.getAbsoluteFilePath(currAbsolutePath.substring(0, currAbsolutePath.lastIndexOf('/')), path.replace('../', '')); return this.getAbsoluteFilePath(currDirNode && currDirNode.parentNode, path.replace('../', ''));
} else if (path.indexOf('./') === 0) { } else if (path.indexOf('./') === 0) {
path = `${currAbsolutePath}/${path.replace('./', '')}`; path = `${currAbsolutePath}/${path.replace('./', '')}`;
} else { } else {
path = `${currAbsolutePath}/${path}`; path = `${currAbsolutePath}/${path}`;
} }
return (path.indexOf('/') === 0 ? path : `/${path}`).replace(' ', '%20'); return (path.indexOf('/') === 0 ? path : `/${path}`).replaceAll(' ', '%20');
}, },
}; };