From cf568d809d50872b94d93d4285948644a24ade90 Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Sat, 12 Aug 2023 21:22:56 -0400 Subject: [PATCH] feat: Adds layout suggestor (close #226) --- src/main.ts | 3 +- src/suggest/index.ts | 92 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 1c36d848..c28cdd8f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -29,7 +29,7 @@ import { import { Watcher } from "./watcher/watcher"; import type { DefaultLayout, Layout, StatblockItem } from "../types/layout"; import { Layout5e } from "./layouts/basic 5e/basic5e"; -import { StatblockSuggester } from "./suggest"; +import { LayoutSuggester, StatblockSuggester } from "./suggest"; import { DefaultLayouts } from "./layouts"; import type { StatblockData } from "index"; @@ -242,6 +242,7 @@ export default class StatBlockPlugin extends Plugin implements StatblockAPI { ); this.registerEditorSuggest(new StatblockSuggester(this)); + this.registerEditorSuggest(new LayoutSuggester(this)); this.registerEvent( this.app.workspace.on("dice-roller:unload", () => { diff --git a/src/suggest/index.ts b/src/suggest/index.ts index 3dfd4f1c..14d5cdd7 100644 --- a/src/suggest/index.ts +++ b/src/suggest/index.ts @@ -99,3 +99,95 @@ export class StatblockSuggester extends EditorSuggest { return matchData; } } +export class LayoutSuggester extends EditorSuggest { + constructor(public plugin: StatBlockPlugin) { + super(plugin.app); + } + getSuggestions(ctx: EditorSuggestContext) { + return this.plugin.layouts + .filter((p) => + p.name.toLowerCase().contains(ctx.query.toLowerCase()) + ) + .map((l) => l.name); + } + renderSuggestion(text: string, el: HTMLElement) { + el.createSpan({ text }); + } + selectSuggestion(value: string, evt: MouseEvent | KeyboardEvent): void { + if (!this.context) return; + + const line = this.context.editor + .getLine(this.context.end.line) + .slice(this.context.end.ch); + const [_, exists] = line.match(/^(\] ?)/) ?? []; + + this.context.editor.replaceRange( + `${value}`, + this.context.start, + { + ...this.context.end, + ch: + this.context.start.ch + + this.context.query.length + + (exists?.length ?? 0) + }, + "statblocks" + ); + + this.context.editor.setCursor( + this.context.start.line, + this.context.start.ch + value.length + ); + + this.close(); + } + onTrigger( + cursor: EditorPosition, + editor: Editor, + file: TFile + ): EditorSuggestTriggerInfo { + const range = editor.getRange({ line: 0, ch: 0 }, cursor); + + if (range.indexOf("```statblock\n") === -1) return; + + const split = range.split("\n").reverse(); + + let inStatblock = false; + for (const line of split) { + if (/^```$/.test(line)) return; + if (/^```statblock/.test(line)) { + inStatblock = true; + break; + } + } + if (!inStatblock) return; + + const line = editor.getLine(cursor.line); + //not inside the bracket + + if (!/^(layout):/m.test(line.slice(0, cursor.ch))) return null; + + const match = line.match(/^(layout): (.+)\n?/); + if (!match) return null; + + const [_, param, query] = match; + + if ( + !query || + this.plugin.layouts.find( + (p) => p.name.toLowerCase() == query.toLowerCase() + ) + ) { + return null; + } + const matchData = { + end: cursor, + start: { + ch: param.length + 2, + line: cursor.line + }, + query + }; + return matchData; + } +}