Skip to content

Commit

Permalink
fix: admonition and callout suggester displays immediately (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Feb 3, 2023
1 parent 689a241 commit 6631412
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 128 deletions.
112 changes: 56 additions & 56 deletions package-lock.json

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

127 changes: 55 additions & 72 deletions src/suggest/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,59 @@ import {
} from "obsidian";
import ObsidianAdmonition from "src/main";

export class CalloutSuggest extends EditorSuggest<string> {
abstract class AdmonitionOrCalloutSuggester extends EditorSuggest<string> {
constructor(public plugin: ObsidianAdmonition) {
super(plugin.app);
}
getSuggestions(ctx: EditorSuggestContext) {
if (!ctx.query?.length) return Object.keys(this.plugin.admonitions);
return Object.keys(this.plugin.admonitions).filter((p) =>
p.toLowerCase().contains(ctx.query.toLowerCase())
);
}
renderSuggestion(text: string, el: HTMLElement) {
el.createSpan({ text });
}
onTrigger(
cursor: EditorPosition,
editor: Editor
): EditorSuggestTriggerInfo {
const line = editor.getLine(cursor.line);
const match = this.testAndReturnQuery(line, cursor);
if (!match) return null;
const [_, query] = match;

if (
Object.keys(this.plugin.admonitions).find(
(p) => p.toLowerCase() == query.toLowerCase()
)
) {
return null;
}

const matchData = {
end: cursor,
start: {
ch: match.index + this.offset,
line: cursor.line
},
query
};
return matchData;
}
abstract offset: number;
abstract selectSuggestion(
value: string,
evt: MouseEvent | KeyboardEvent
): void;
abstract testAndReturnQuery(
line: string,
cursor: EditorPosition
): RegExpMatchArray | null;
}

export class CalloutSuggest extends AdmonitionOrCalloutSuggester {
offset = 4;
selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
if (!this.context) return;

Expand Down Expand Up @@ -48,52 +89,17 @@ export class CalloutSuggest extends EditorSuggest<string> {

this.close();
}
onTrigger(
cursor: EditorPosition,
editor: Editor,
file: TFile
): EditorSuggestTriggerInfo {
const line = editor.getLine(cursor.line);
//not inside the bracket
testAndReturnQuery(
line: string,
cursor: EditorPosition
): RegExpMatchArray | null {
if (/> \[!\w+\]/.test(line.slice(0, cursor.ch))) return null;
if (!/> \[!\w*/.test(line)) return null;

const match = line.match(/> \[!(\w*)\]?/);
if (!match) return null;

const [_, query] = match;

if (
!query ||
Object.keys(this.plugin.admonitions).find(
(p) => p.toLowerCase() == query.toLowerCase()
)
) {
return null;
}
const matchData = {
end: cursor,
start: {
ch: match.index + 4,
line: cursor.line
},
query
};
return matchData;
return line.match(/> \[!(\w*)\]?/);
}
}
export class AdmonitionSuggest extends EditorSuggest<string> {
constructor(public plugin: ObsidianAdmonition) {
super(plugin.app);
}
getSuggestions(ctx: EditorSuggestContext) {
return Object.keys(this.plugin.admonitions).filter((p) =>
p.toLowerCase().contains(ctx.query.toLowerCase())
);
}
renderSuggestion(text: string, el: HTMLElement) {
el.createSpan({ text });
}
export class AdmonitionSuggest extends AdmonitionOrCalloutSuggester {
offset = 6;
selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void {
if (!this.context) return;

Expand All @@ -106,34 +112,11 @@ export class AdmonitionSuggest extends EditorSuggest<string> {

this.close();
}
onTrigger(
cursor: EditorPosition,
editor: Editor,
file: TFile
): EditorSuggestTriggerInfo {
const line = editor.getLine(cursor.line);
if (!/```ad-\w+/.test(line)) return null;
const match = line.match(/```ad-(\w+)/);
if (!match) return null;
const [_, query] = match;

if (
!query ||
Object.keys(this.plugin.admonitions).find(
(p) => p.toLowerCase() == query.toLowerCase()
)
) {
return null;
}

const matchData = {
end: cursor,
start: {
ch: match.index + 6,
line: cursor.line
},
query
};
return matchData;
testAndReturnQuery(
line: string,
cursor: EditorPosition
): RegExpMatchArray | null {
if (!/```ad-\w*/.test(line)) return null;
return line.match(/```ad-(\w*)/);
}
}

0 comments on commit 6631412

Please sign in to comment.