Skip to content

Commit

Permalink
feat: 1.支持emmet模式(目前输入t+Tab可以生成todo 输入tm*n可以生成m*n的表格)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuckZ committed Apr 20, 2023
1 parent 773e03b commit d5777be
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import { EditorUtil, EditorUtils } from '@/utils/editor';
import t from '@/i18n';
import { usePomodoroStore, useSystemStore } from '@/stores';
import { UpdateModal } from '@/ui/modal/UpdateModal';
import { generateMarkdownTable } from '@/utils/table';

export const OpenUrl = ref('https://baidu.com');
const media = window.matchMedia('(prefers-color-scheme: dark)');
Expand Down Expand Up @@ -101,6 +102,7 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
this.editorChangeFunction = this.customizeEditorChange.bind(this);
this.editorPasteFunction = this.customizeEditorPaste.bind(this);
this.fileMenuFunction = this.customizeFileMenu.bind(this);
this.codemirrorFunction = this.customizeCodeMirror.bind(this);
this.vaultCreateFunction = this.customizeVaultCreate.bind(this);
this.vaultModifyFunction = this.customizeVaultModify.bind(this);
this.vaultDeleteFunction = this.customizeVaultDelete.bind(this);
Expand Down Expand Up @@ -244,6 +246,10 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
});
}

async customizeCodeMirror(cm: CodeMirror.Editor, view: MarkdownView): Promise<void> {
// LoggerUtil.log('');
}

async customizeVaultCreate(file: TAbstractFile): Promise<void> {
// LoggerUtil.log('');
}
Expand Down Expand Up @@ -570,6 +576,25 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
media.addEventListener('change', callback);
this.register(() => media.removeEventListener('change', callback));
this.register(() => mutationObserver.disconnect());
window.onkeydown = event => {
if (event.key === 'Tab') {
const editor = this.app.workspace.activeEditor?.editor as Editor;
if (editor) {
let triggerText = EditorUtils.getCurrentSelection(editor);
triggerText = triggerText.trim();
if (triggerText === 't') {
const targetText = '- [ ] ';
EditorUtils.replaceCurrentSelection(editor, targetText);
}
const tableConfig = triggerText.match(/^t(\d+)\*(\d+)/);
if (tableConfig) {
const targetText = generateMarkdownTable(tableConfig[1], tableConfig[2]);
EditorUtils.replaceCurrentSelection(editor, targetText);
}
}
event.preventDefault();
}
};
// window.addEventListener('languagechange', () => {
// console.log('languagechange event detected!');
// });
Expand All @@ -581,6 +606,7 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
});
window.addEventListener(eventTypes.openBrowser, this.openBrowserHandle.bind(this));
[
this.app.workspace.on('codemirror', this.codemirrorFunction),
this.app.workspace.on('click', this.clickFunction),
this.app.workspace.on('resize', this.resizeFunction),
this.app.workspace.on('editor-change', this.editorChangeFunction),
Expand Down
6 changes: 6 additions & 0 deletions src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export class EditorUtils {
return content;
}

static replaceCurrentSelection(editor: Editor, targetText: string) {
const cursorPos = editor.getCursor();
const line = editor.getLine(cursorPos.line);
editor.replaceRange(targetText, { line: cursorPos.line, ch: 0 }, { line: cursorPos.line, ch: line.length });
}

unload() {
if (this.ele) {
document.body.removeChild(this.ele);
Expand Down
26 changes: 26 additions & 0 deletions src/utils/table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function generateMarkdownTable(row, col) {
// Initialize table header
let tableHeader = '|';
for (let i = 1; i <= col; i++) {
tableHeader += ` ${i} |`;
}

// Initialize separator
let separator = '|';
for (let i = 1; i <= col; i++) {
separator += '---|';
}

// Initialize table content
let tableContent = '';
for (let i = 1; i <= row; i++) {
let rowContent = '|';
for (let j = 1; j <= col; j++) {
rowContent += ` ${i}-${j} |`;
}
tableContent += `${rowContent}\n`;
}

// Combine header, separator, and content to form the Markdown table
return `${tableHeader}\n${separator}\n${tableContent}`;
}

0 comments on commit d5777be

Please sign in to comment.