Skip to content

Commit

Permalink
feat: Adds layout suggestor (close #226)
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Aug 13, 2023
1 parent 78fa44e commit cf568d8
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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", () => {
Expand Down
92 changes: 92 additions & 0 deletions src/suggest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,95 @@ export class StatblockSuggester extends EditorSuggest<string> {
return matchData;
}
}
export class LayoutSuggester extends EditorSuggest<string> {
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;
}
}

0 comments on commit cf568d8

Please sign in to comment.