支持笔记之间双链

This commit is contained in:
xiaoqi.cxq 2022-11-20 15:11:31 +08:00
parent 31bec53520
commit 13b9528840
7 changed files with 87 additions and 0 deletions

View File

@ -22,6 +22,8 @@ import networkSvc from '../services/networkSvc';
import tempFileSvc from '../services/tempFileSvc';
import store from '../store';
import './common/vueGlobals';
import utils from '../services/utils';
import providerRegistry from '../services/providers/common/providerRegistry';
const themeClasses = {
light: ['app--light'],
@ -49,8 +51,40 @@ export default {
close() {
tempFileSvc.close();
},
//
viewFileByPath(path) {
// md
if (!path) {
return;
}
const currDirNode = store.getters['explorer/selectedNodeFolder'];
if (path.slice(-3) === '.md') {
const rootNode = store.getters['explorer/rootNode'];
const node = utils.findNodeByPath(rootNode, currDirNode, path);
if (!node) {
return;
}
store.commit('explorer/setSelectedId', node.item.id);
// Prevent from freezing the UI while loading the file
setTimeout(() => {
store.commit('file/setCurrentId', node.item.id);
}, 10);
} else {
const workspace = store.getters['workspace/currentWorkspace'];
const provider = providerRegistry.providersById[workspace.providerId];
if (provider == null) {
return;
}
const absolutePath = utils.getAbsoluteFilePath(currDirNode, path);
const url = provider.getFilePathUrl(absolutePath);
if (url) {
window.open(url, '_blank');
}
}
},
},
async created() {
window.viewFileByPath = this.viewFileByPath;
try {
await syncSvc.init();
await networkSvc.init();

View File

@ -216,6 +216,15 @@ const editorSvc = Object.assign(new Vue(), editorSvcDiscussions, editorSvcUtils,
...imgs,
];
Array.prototype.slice.call(sectionPreviewElt.getElementsByTagName('a')).forEach((aElt) => {
const url = aElt.attributes.href.nodeValue;
if (url.indexOf('http://') >= 0 || url.indexOf('https://') >= 0) {
return;
}
aElt.href = 'javascript:void(0);'; // eslint-disable-line no-script-url
aElt.setAttribute('onclick', `window.viewFileByPath('${utils.decodeUrlPath(url)}')`);
});
// Create TOC section element
sectionTocElt = document.createElement('div');
sectionTocElt.className = 'cl-toc-section';

View File

@ -320,4 +320,12 @@ export default new Provider({
});
return Provider.parseContent(data, contentId);
},
getFilePathUrl(path) {
const token = this.getToken();
if (!token) {
return null;
}
const workspace = store.getters['workspace/currentWorkspace'];
return `${token.serverUrl}/${workspace.owner}/${workspace.repo}/src/branch/${workspace.branch}${path}`;
},
});

View File

@ -282,4 +282,11 @@ export default new Provider({
});
return Provider.parseContent(data, contentId);
},
getFilePathUrl(path) {
const token = this.getToken();
if (!token) {
return null;
}
return `https://gitee.com/${token.name}/${appDataRepo}/blob/${appDataBranch}${path}`;
},
});

View File

@ -308,4 +308,8 @@ export default new Provider({
});
return Provider.parseContent(data, contentId);
},
getFilePathUrl(path) {
const workspace = store.getters['workspace/currentWorkspace'];
return `https://gitee.com/${workspace.owner}/${workspace.repo}/blob/${workspace.branch}${path}`;
},
});

View File

@ -306,4 +306,8 @@ export default new Provider({
});
return Provider.parseContent(data, contentId);
},
getFilePathUrl(path) {
const workspace = store.getters['workspace/currentWorkspace'];
return `https://github.com/${workspace.owner}/${workspace.repo}/blob/${workspace.branch}${path}`;
},
});

View File

@ -306,6 +306,9 @@ export default {
encodeUrlPath(path) {
return path ? path.split('/').map(encodeURIComponent).join('/') : '';
},
decodeUrlPath(path) {
return path ? path.split('/').map(decodeURIComponent).join('/') : '';
},
parseGithubRepoUrl(url) {
const parsedRepo = url && url.match(/([^/:]+)\/([^/]+?)(?:\.git|\/)?$/);
return parsedRepo && {
@ -414,4 +417,22 @@ export default {
}
return (path.indexOf('/') === 0 ? path : `/${path}`).replaceAll(' ', '%20');
},
findNodeByPath(rootNode, currDirNode, filePath) {
// 先获取绝对路径
const path = this.getAbsoluteFilePath(currDirNode, filePath).replaceAll('%20', ' ');
const pathArr = path.split('/');
let node = rootNode;
for (let i = 0; i < pathArr.length; i += 1) {
if (i > 0) {
if (i === pathArr.length - 1) {
return node.files.find(it => `${it.item.name}.md` === pathArr[i]);
}
node = node.folders.find(it => it.item.name === pathArr[i]);
if (!node) {
return null;
}
}
}
return null;
},
};