mirror of
https://github.com/rabirabirara/obsidian-jelly-snippets.git
synced 2026-07-22 07:30:22 +00:00
clean: format and add comment
This commit is contained in:
parent
ce26634007
commit
9f559b93b6
1 changed files with 71 additions and 49 deletions
44
main.ts
44
main.ts
|
|
@ -78,7 +78,7 @@ export default class JellySnippets extends Plugin {
|
|||
) {
|
||||
const onKeyEvent = (evt: KeyboardEvent) => {
|
||||
if (
|
||||
(!evt.shiftKey) // TODO: add function to determine when not to trigger. don't trigger if shift is pressed down as well e.g.
|
||||
!evt.shiftKey // TODO: add function to determine when not to trigger. don't trigger if shift is pressed down as well e.g.
|
||||
) {
|
||||
const mdFile = this.app.workspace.activeEditor;
|
||||
if (mdFile?.editor) {
|
||||
|
|
@ -91,9 +91,11 @@ export default class JellySnippets extends Plugin {
|
|||
this.registerDomEvent(document, "keydown", onKeyEvent);
|
||||
|
||||
// If window changes, registerDomEvent for new window if so.
|
||||
this.registerEvent(this.app.workspace.on('window-open', (event) => {
|
||||
this.registerEvent(
|
||||
this.app.workspace.on("window-open", (event) => {
|
||||
this.registerDomEvent(activeWindow, "keydown", onKeyEvent);
|
||||
}));
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
this.addCommand({
|
||||
|
|
@ -169,6 +171,7 @@ export default class JellySnippets extends Plugin {
|
|||
// }
|
||||
|
||||
triggerSearchSnippetAutomatically(editor: Editor, evt: KeyboardEvent) {
|
||||
// remember the order: enter and tab come out before code triggers, space comes out after.
|
||||
switch (evt.key) {
|
||||
case " ": {
|
||||
if (this.settings.triggerOnSpace !== AutoTriggerOptions.Disabled) {
|
||||
|
|
@ -200,12 +203,17 @@ export default class JellySnippets extends Plugin {
|
|||
let abovelineEnd = editor.getLine(aboveline).length;
|
||||
let peekPos: EditorPosition = { line: aboveline, ch: abovelineEnd };
|
||||
if (this.triggerSearchSnippet(editor, peekPos)) {
|
||||
if (this.settings.triggerOnEnter === AutoTriggerOptions.EnabledNoWS) {
|
||||
if (
|
||||
this.settings.triggerOnEnter === AutoTriggerOptions.EnabledNoWS
|
||||
) {
|
||||
// undo the already created newline by deleting everything from curpos to above line's end
|
||||
// yes, you need to recalculate the above line's end else it will use an incorrect position
|
||||
let aboveLine = editor.getCursor().line - 1;
|
||||
let aboveLineEnd = editor.getLine(aboveLine).length;
|
||||
let aboveLineEndPos: EditorPosition = { line: aboveLine, ch: aboveLineEnd };
|
||||
let aboveLineEndPos: EditorPosition = {
|
||||
line: aboveLine,
|
||||
ch: aboveLineEnd,
|
||||
};
|
||||
editor.replaceRange("", aboveLineEndPos, curpos);
|
||||
}
|
||||
return true;
|
||||
|
|
@ -219,7 +227,10 @@ export default class JellySnippets extends Plugin {
|
|||
return false;
|
||||
}
|
||||
|
||||
triggerSearchSnippet(editor: Editor, pos: EditorPosition | null = null): boolean {
|
||||
triggerSearchSnippet(
|
||||
editor: Editor,
|
||||
pos: EditorPosition | null = null,
|
||||
): boolean {
|
||||
// uses prepareSimpleSearch to search for matches that end right at cursor.
|
||||
// basically, get text from start of line to cursor, do simple search for each snippet lhs in that text, and replace first match that ends right at cursor.
|
||||
|
||||
|
|
@ -237,7 +248,10 @@ export default class JellySnippets extends Plugin {
|
|||
if (curpos.ch >= lhs.length) {
|
||||
// TODO: This change fixes old bug but snippet now triggers even if there is no whitespace before the lhs. Or does it? Verify this...
|
||||
// TODO: lhs still cannot have newlines in it. Wouldn't be hard to update however.
|
||||
let from: EditorPosition = { line: line, ch: curpos.ch - lhs.length };
|
||||
let from: EditorPosition = {
|
||||
line: line,
|
||||
ch: curpos.ch - lhs.length,
|
||||
};
|
||||
let to: EditorPosition = { line: line, ch: lastMatchPart[1] };
|
||||
let lookBack = editor.getRange(from, to);
|
||||
if (lookBack === lhs) {
|
||||
|
|
@ -324,7 +338,10 @@ class JellySnippetsSettingTab extends PluginSettingTab {
|
|||
dropdown
|
||||
.addOption(AutoTriggerOptions.Disabled, "Disabled")
|
||||
// .addOption(AutoTriggerOptions.EnabledNoWS, "Enabled, no whitespace")
|
||||
.addOption(AutoTriggerOptions.EnabledYesWS, "Enabled, also whitespace")
|
||||
.addOption(
|
||||
AutoTriggerOptions.EnabledYesWS,
|
||||
"Enabled, also whitespace",
|
||||
)
|
||||
.setValue(this.plugin.settings.triggerOnSpace)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.triggerOnSpace = value as AutoTriggerOptions;
|
||||
|
|
@ -341,7 +358,10 @@ class JellySnippetsSettingTab extends PluginSettingTab {
|
|||
dropdown
|
||||
.addOption(AutoTriggerOptions.Disabled, "Disabled")
|
||||
.addOption(AutoTriggerOptions.EnabledNoWS, "Enabled, no whitespace")
|
||||
.addOption(AutoTriggerOptions.EnabledYesWS, "Enabled, also whitespace")
|
||||
.addOption(
|
||||
AutoTriggerOptions.EnabledYesWS,
|
||||
"Enabled, also whitespace",
|
||||
)
|
||||
.setValue(this.plugin.settings.triggerOnEnter)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.triggerOnEnter = value as AutoTriggerOptions;
|
||||
|
|
@ -358,7 +378,10 @@ class JellySnippetsSettingTab extends PluginSettingTab {
|
|||
dropdown
|
||||
.addOption(AutoTriggerOptions.Disabled, "Disabled")
|
||||
.addOption(AutoTriggerOptions.EnabledNoWS, "Enabled, no whitespace")
|
||||
.addOption(AutoTriggerOptions.EnabledYesWS, "Enabled, also whitespace")
|
||||
.addOption(
|
||||
AutoTriggerOptions.EnabledYesWS,
|
||||
"Enabled, also whitespace",
|
||||
)
|
||||
.setValue(this.plugin.settings.triggerOnTab)
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.triggerOnTab = value as AutoTriggerOptions;
|
||||
|
|
@ -367,4 +390,3 @@ class JellySnippetsSettingTab extends PluginSettingTab {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue