Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.
/ liandi Public archive

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Apr 30, 2020
2 parents b8a1554 + 123fe8d commit 08a09d6
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 29 deletions.
6 changes: 3 additions & 3 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@
"webpack-cli": "^3.3.10"
},
"dependencies": {
"vditor": "^3.1.6"
"vditor": "^3.1.20"
}
}
4 changes: 2 additions & 2 deletions app/src/config/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const image = {
});
});
},
onSetimage: (liandi: ILiandi, image: IImage) => {
liandi.config.image = image;
onSetimage: (liandi: ILiandi, imageConfig: IImage) => {
liandi.config.image = imageConfig;
}
};
2 changes: 1 addition & 1 deletion app/src/config/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const markdown = {
<label>
<input value="ir" name="editMode" type="radio"${liandi.config.markdown.editorMode === 'ir' ? ' checked' : ''}/>
<span class="fn__space"></span>${i18n[liandi.config.lang].ir}
</label>
</label>
<span class="fn__space"></span><span class="fn__space"></span>
<label>
<input value="sv" name="editMode" type="radio"${liandi.config.markdown.editorMode === 'sv' ? ' checked' : ''}/>
Expand Down
4 changes: 3 additions & 1 deletion app/src/config/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export const initConfigSearch = (liandi: ILiandi, element: HTMLElement) => {
return;
}
if (event.key === 'Escape') {
destroyDialog();
destroyDialog(() => {
liandi.editors.focus();
});
event.preventDefault();
}
});
Expand Down
1 change: 1 addition & 0 deletions app/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export abstract class Constants {
public static readonly LIANDI_EDITOR_OPEN: string = 'liandi-editor-open';
public static readonly LIANDI_EDITOR_FULLSCREEN: string = 'liandi-editor-fullscreen';
public static readonly LIANDI_EDITOR_RESTORE: string = 'liandi-editor-restore';
public static readonly LIANDI_EDITOR_CURSOR: string = 'liandi-editor-cursor';

public static readonly LIANDI_WEBSOCKET_PUT: string = 'liandi-websocket-put';

Expand Down
10 changes: 8 additions & 2 deletions app/src/editors/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {rename} from '../util/rename';
import {remote} from 'electron';
import {Constants} from '../constants';

export class Editors {
private inputElement: HTMLInputElement;
Expand Down Expand Up @@ -34,10 +35,15 @@ export class Editors {
this.editorWebviewElement.classList.add('fn__none');
}

sendMessage(message: string, liandi: ILiandi, editorData?: { content: string, name: string }) {
focus() {
this.editorWebviewElement.focus();
this.sendMessage(Constants.LIANDI_EDITOR_CURSOR);
}

sendMessage(message: string, liandi?: ILiandi, editorData?: { content: string, name: string }) {
if (editorData) {
remote.getGlobal('liandiEditor').editorText = editorData.content;
this.inputElement.value = editorData.name.replace(".md", "");
this.inputElement.value = editorData.name.replace('.md', '');
this.inputElement.classList.remove('fn__none');
this.editorWebviewElement.classList.remove('fn__none');
}
Expand Down
36 changes: 26 additions & 10 deletions app/src/editors/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ const Vditor = require('vditor');
export class EditorWebview {
private isInitMenu: boolean;
private vditor: any;
private range: Range;

constructor() {
this.isInitMenu = false;
initGlobalKeyPress();
this.range = document.createRange();
initGlobalKeyPress(this);
this.onMessage();
if (process.platform === 'win32') {
document.body.classList.add('body--win32');
Expand Down Expand Up @@ -41,7 +43,7 @@ export class EditorWebview {
id: 'copyAsPlainText',
accelerator: 'CmdOrCtrl+Shift+C',
click: () => {
clipboard.writeText(getSelection().getRangeAt(0).toString().replace(//g, ""));
clipboard.writeText(getSelection().getRangeAt(0).toString().replace(//g, ''));
}
}));
menu.append(new remote.MenuItem({
Expand Down Expand Up @@ -88,10 +90,15 @@ export class EditorWebview {
ipcRenderer.on(Constants.LIANDI_EDITOR_RELOAD, (event, data) => {
this.onOpen(data);
});
ipcRenderer.on(Constants.LIANDI_EDITOR_CURSOR, (event, data) => {
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(this.range);
});
}

private isCtrl(event: KeyboardEvent) {
if (navigator.platform.toUpperCase().indexOf("MAC") >= 0) {
if (navigator.platform.toUpperCase().indexOf('MAC') >= 0) {
// mac
if (event.metaKey && !event.ctrlKey) {
return true;
Expand All @@ -103,7 +110,7 @@ export class EditorWebview {
}
return false;
}
};
}

private onOpen(liandi: ILiandi, value: string = remote.getGlobal('liandiEditor').editorText) {
document.getElementById('liandiVditor').innerHTML = '';
Expand Down Expand Up @@ -132,6 +139,8 @@ export class EditorWebview {
'line',
'code',
'inline-code',
'insert-before',
'insert-after',
'|',
'upload',
'table',
Expand Down Expand Up @@ -162,6 +171,7 @@ export class EditorWebview {
}
},
},
'outline',
'devtools',
'info',
'help',
Expand All @@ -171,12 +181,16 @@ export class EditorWebview {
cache: {
enable: false
},
cdn: remote.getGlobal('liandiEditor').appDir + "/node_modules/vditor",
counter: {
enable: true
},
cdn: remote.getGlobal('liandiEditor').appDir + '/node_modules/vditor',
preview: {
markdown: {
autoSpace: liandi.config.markdown.autoSpace,
chinesePunct: liandi.config.markdown.chinesePunct,
fixTermTypo: liandi.config.markdown.fixTermTypo,
theme: liandi.config.theme,
toc: liandi.config.markdown.toc,
footnotes: liandi.config.markdown.footnotes,
setext: liandi.config.markdown.setext
Expand All @@ -189,13 +203,14 @@ export class EditorWebview {
style: liandi.config.theme === 'dark' ? 'native' : 'github'
}
},
height: window.innerHeight - 20,
upload: {
setHeaders: () => {
return {
'X-URL': encodeURIComponent(liandi.current.dir.url),
'X-PATH': encodeURIComponent(liandi.current.path),
'X-Mode': this.vditor.getCurrentMode()
}
};
},
linkToImgUrl: Constants.UPLOAD_FETCH_ADDRESS,
filename: (name: string) => name.replace(/[^(a-zA-Z0-9\u4e00-\u9fa5\.)]/g, '').replace(/[\?\\/:|<>\*\[\]\(\)\$%\{\}@~]/g, '').replace('/\\s/g', ''),
Expand All @@ -206,6 +221,7 @@ export class EditorWebview {
this.vditor.setValue(value);
remote.getGlobal('liandiEditor').editorText = value;
remote.getGlobal('liandiEditor').saved = true;
this.vditor.focus();
},
input: (textContent: string) => {
remote.getGlobal('liandiEditor').editorText = textContent;
Expand All @@ -217,19 +233,19 @@ export class EditorWebview {
}
});

this.vditor.vditor.wysiwyg.element.addEventListener("keydown", (event: KeyboardEvent) => {
this.vditor.vditor.wysiwyg.element.addEventListener('keydown', (event: KeyboardEvent) => {
if (this.isCtrl(event) && event.key.toLowerCase() === 'v' && !event.altKey && event.shiftKey) {
const range = getSelection().getRangeAt(0)
const range = getSelection().getRangeAt(0);
range.extractContents();
this.vditor.insertValue(clipboard.readText());
event.preventDefault();
}

if (this.isCtrl(event) && event.key.toLowerCase() === 'c' && !event.altKey && event.shiftKey) {
clipboard.writeText(getSelection().getRangeAt(0).toString().replace(//g, ""));
clipboard.writeText(getSelection().getRangeAt(0).toString().replace(//g, ''));
event.preventDefault();
}
})
});
}
}

Expand Down
11 changes: 6 additions & 5 deletions app/src/hotkey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import {Constants} from '../constants';
import {ipcRenderer} from 'electron';
import {initSearch} from '../search';

export const initGlobalKeyPress = (liandi?: ILiandi) => {
export const initGlobalKeyPress = (liandi: ILiandi | any) => {
let lastKeypressTime = 0;

window.addEventListener('keyup', (event) => {
window.addEventListener('keydown', (event) => {
// 快捷搜素
if (event.key === 'Shift') {
const thisKeypressTime = new Date().getTime();
if (thisKeypressTime - lastKeypressTime <= Constants.DOUBLE_DELTA
&& thisKeypressTime - lastKeypressTime >= 50) { // 防止 win32 长按
lastKeypressTime = 0;
if (liandi) {
initSearch(liandi);
} else {
if (liandi.range) {
liandi.range = getSelection().getRangeAt(0);
ipcRenderer.sendToHost(Constants.LIANDI_SEARCH_OPEN);
} else {
initSearch(liandi);
}
}
lastKeypressTime = thisKeypressTime;
Expand Down
2 changes: 1 addition & 1 deletion app/src/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const i18n: II18n = {
copyAsPlainText: 'Copy As Plain Text',
paste: 'Paste',
pasteAsPlainText: 'Paste As Plain Text',
hideToolbar: "Hide Editor's toolbar",
hideToolbar: 'Hide Editor\'s toolbar',
toc: 'Enable [toc]',
footnotes: 'Enable [^footnotes]',
autoFetch: 'Auto fetch remote image to local',
Expand Down
10 changes: 8 additions & 2 deletions app/src/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {initConfigSearch} from '../config/search';
import {getPath, removeLastPath} from '../util/path';
import {markdown} from '../config/markdown';
import {image} from '../config/image';
import {Constants} from '../constants';

export const quickOpenFile = (liandi: ILiandi, dialogElement: Element) => {
const currentList: HTMLElement = dialogElement.querySelector('div[data-name="search"] .list__item--current');
Expand Down Expand Up @@ -76,7 +77,10 @@ export const initSearch = (liandi: ILiandi) => {
</div>
</tab-panel>`,
width: Math.max(window.innerWidth - 520, 600),
height: 520
height: 520,
destroyDialogCallback: () => {
liandi.editors.focus();
}
});

const dialogElement = document.querySelector('#dialog');
Expand All @@ -102,7 +106,9 @@ export const initSearch = (liandi: ILiandi) => {
return;
}
if (event.key === 'Escape') {
destroyDialog();
destroyDialog(() => {
liandi.editors.focus();
});
event.preventDefault();
}
let currentList: HTMLElement = dialogElement.querySelector('div[data-name="search"] .list__item--current');
Expand Down
1 change: 1 addition & 0 deletions app/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ declare interface ILiandi {
onRename: (liandi: ILiandi, data: { newPath: string, oldPath: string, newName: string }) => void
};
editors?: {
focus: () => void;
save: (liandi:ILiandi) => void;
close: (liandi:ILiandi) => void;
sendMessage: (message: string, liandi: ILiandi, editorData?: { content: string, name: string }) => void;
Expand Down
2 changes: 1 addition & 1 deletion app/src/util/dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const dialog = (options: {
if (!dialogElement) {
document.body.insertAdjacentHTML('beforeend', `
<div class="dialog" id="dialog">
${options.hideBackground ? "" : '<div class="dialog__bg"></div>'}
${options.hideBackground ? '' : '<div class="dialog__bg"></div>'}
<div class="dialog__main fn__layer">
<svg class="dialog__close"><use xlink:href="#iconClose"></use></svg>
<div class="dialog__header" onselectstart="return false;">
Expand Down
1 change: 1 addition & 0 deletions app/src/websocket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class WebSocketUtil {
liandi.files.onLs(liandi, response.data);
break;
case 'get':
liandi.editors.focus();
liandi.editors.sendMessage(Constants.LIANDI_EDITOR_OPEN, liandi, response.data);
break;
case 'searchget':
Expand Down

0 comments on commit 08a09d6

Please sign in to comment.