Fix some nasty bug

pull/3/head
Zef Hemel 2022-04-01 15:02:35 +02:00
parent 4d201efdbb
commit b858917c98
13 changed files with 115 additions and 37 deletions

View File

@ -37,9 +37,8 @@ self.syscall = async (name: string, ...args: any[]) => {
}); });
}; };
function wrapScript(code: string): string { function wrapScript(code: string) {
return `const fn = ${code}; return `return (${code})["default"]`;
return fn["default"].apply(null, arguments);`;
} }
self.addEventListener("message", (event: { data: WorkerMessage }) => { self.addEventListener("message", (event: { data: WorkerMessage }) => {
@ -47,7 +46,8 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => {
let data = event.data; let data = event.data;
switch (data.type) { switch (data.type) {
case "load": case "load":
loadedFunctions.set(data.name!, new Function(wrapScript(data.code!))); let fn2 = new Function(wrapScript(data.code!));
loadedFunctions.set(data.name!, fn2());
postMessage( postMessage(
{ {
type: "inited", type: "inited",

View File

@ -79,8 +79,8 @@ export async function updateMaterializedQueriesOnPage(pageName: string) {
for (let { for (let {
key, key,
page, page,
value: {item, children}, value: { item, children },
} of await syscall("index.scanPrefixGlobal", "it:")) { }; of await syscall("index.scanPrefixGlobal", "it:")) {
let [, pos] = key.split(":"); let [, pos] = key.split(":");
if (!filter || (filter && item.includes(filter))) { if (!filter || (filter && item.includes(filter))) {
results.push(`* [[${page}@${pos}]] ${item}`); results.push(`* [[${page}@${pos}]] ${item}`);

1
plugs/emoji/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
emoji-data.txt

3
plugs/emoji/Makefile Normal file
View File

@ -0,0 +1,3 @@
build:
curl https://unicode.org/Public/emoji/14.0/emoji-test.txt > emoji-data.txt
node build.js

17
plugs/emoji/build.js Normal file
View File

@ -0,0 +1,17 @@
// Generates emoji.json from emoji-data.txt
const { readFileSync, writeFileSync } = require("fs");
const emojiRe = /#\s([^\s]+)\s+E[^\s]+\s+(.+)$/;
let text = readFileSync("emoji-data.txt", "utf-8");
const lines = text.split("\n").filter((line) => !line.startsWith("#"));
let emoji = [];
for (const line of lines) {
let match = emojiRe.exec(line);
if (match) {
emoji.push([match[1], match[2].toLowerCase().replaceAll(/\W+/g, "_")]);
}
}
writeFileSync("emoji.json", JSON.stringify(emoji));

View File

@ -0,0 +1,4 @@
functions:
emojiCompleter:
path: "./emoji.ts:emojiCompleter"
isCompleter: true

26
plugs/emoji/emoji.ts Normal file
View File

@ -0,0 +1,26 @@
// @ts-ignore
import emojis from "./emoji.json";
import { syscall } from "../lib/syscall";
const emojiMatcher = /\(([^\)]+)\)\s+(.+)$/;
export async function emojiCompleter() {
let prefix = await syscall("editor.matchBefore", ":[\\w\\s]*");
if (!prefix) {
return null;
}
const textPrefix = prefix.text.substring(1); // Cut off the initial :
let filteredEmoji = emojis.filter(([_, shortcode]) =>
shortcode.includes(textPrefix)
);
return {
from: prefix.from,
filter: false,
options: filteredEmoji.map(([emoji, shortcode]) => ({
detail: shortcode,
label: emoji,
type: "emoji",
})),
};
}

View File

@ -86,7 +86,7 @@ export class ExpressServer {
res.status(200); res.status(200);
res.send(""); res.send("");
} }
};) })
.put(bodyParser.text({ type: "*/*" }), async (req, res) => { .put(bodyParser.text({ type: "*/*" }), async (req, res) => {
let pageName = req.params[0]; let pageName = req.params[0];
console.log("Saving", pageName); console.log("Saving", pageName);

View File

@ -51,6 +51,13 @@ function fuzzyFilter(pattern: string, options: Option[]): Option[] {
return matches; return matches;
} }
function simpleFilter(pattern: string, options: Option[]): Option[] {
const lowerPattern = pattern.toLowerCase();
return options.filter((option) => {
return option.name.toLowerCase().includes(lowerPattern);
});
}
export function FilterList({ export function FilterList({
placeholder, placeholder,
options, options,
@ -59,6 +66,7 @@ export function FilterList({
onKeyPress, onKeyPress,
allowNew = false, allowNew = false,
helpText = "", helpText = "",
completePrefix,
icon, icon,
newHint, newHint,
}: { }: {
@ -68,6 +76,7 @@ export function FilterList({
onKeyPress?: (key: string, currentText: string) => void; onKeyPress?: (key: string, currentText: string) => void;
onSelect: (option: Option | undefined) => void; onSelect: (option: Option | undefined) => void;
allowNew?: boolean; allowNew?: boolean;
completePrefix?: string;
helpText: string; helpText: string;
newHint?: string; newHint?: string;
icon?: IconDefinition; icon?: IconDefinition;
@ -90,7 +99,7 @@ export function FilterList({
if (searchPhrase) { if (searchPhrase) {
let foundExactMatch = false; let foundExactMatch = false;
let results = fuzzyFilter(searchPhrase, options); let results = simpleFilter(searchPhrase, options);
results = results.sort(magicSorter); results = results.sort(magicSorter);
if (allowNew && !foundExactMatch) { if (allowNew && !foundExactMatch) {
results.push({ results.push({
@ -139,7 +148,7 @@ export function FilterList({
ref={searchBoxRef} ref={searchBoxRef}
onChange={filterUpdate} onChange={filterUpdate}
onKeyDown={(e: React.KeyboardEvent) => { onKeyDown={(e: React.KeyboardEvent) => {
// console.log("Key up", e.key); // console.log("Key up", e);
if (onKeyPress) { if (onKeyPress) {
onKeyPress(e.key, text); onKeyPress(e.key, text);
} }
@ -159,6 +168,12 @@ export function FilterList({
case "Escape": case "Escape":
onSelect(undefined); onSelect(undefined);
break; break;
case " ":
if (completePrefix) {
setText(completePrefix);
e.preventDefault();
}
break;
} }
}} }}
/> />

View File

@ -27,6 +27,11 @@ export function PageNavigator({
orderId: orderId, orderId: orderId,
}); });
} }
let completePrefix: string | undefined = undefined;
if (currentPage && currentPage.includes("/")) {
const pieces = currentPage.split("/");
completePrefix = pieces.slice(0, pieces.length - 1).join("/") + "/";
}
return ( return (
<FilterList <FilterList
placeholder="Page" placeholder="Page"
@ -36,6 +41,7 @@ export function PageNavigator({
allowNew={true} allowNew={true}
helpText="Start typing the page name to filter results, press <code>Return</code> to open." helpText="Start typing the page name to filter results, press <code>Return</code> to open."
newHint="Create page" newHint="Create page"
completePrefix={completePrefix}
onSelect={(opt) => { onSelect={(opt) => {
onNavigate(opt?.name); onNavigate(opt?.name);
}} }}

View File

@ -24,17 +24,20 @@ export class CompleterHook implements Hook<CompleterHookT> {
} }
} }
} }
let allCompletionResults = await Promise.all(completerPromises); let actualResult = null;
if (allCompletionResults.length === 1) { for (const result of await Promise.all(completerPromises)) {
return allCompletionResults[0]; if (result) {
} else if (allCompletionResults.length > 1) { if (actualResult) {
console.error( console.error(
"Got completion results from multiple sources, cannot deal with that", "Got completion results from multiple sources, cannot deal with that"
allCompletionResults
); );
}
return null; return null;
} }
actualResult = result;
}
}
return actualResult;
}
apply(system: System<CompleterHookT>): void { apply(system: System<CompleterHookT>): void {
this.system = system; this.system = system;

View File

@ -12,12 +12,12 @@ export class PathPageNavigator {
navigationResolve?: () => void; navigationResolve?: () => void;
async navigate(page: string, pos?: number) { async navigate(page: string, pos?: number) {
window.history.pushState( window.history.pushState({ page, pos }, page, `/${encodePageUrl(page)}`);
{ page, pos }, window.dispatchEvent(
page, new PopStateEvent("popstate", {
`/${encodePageUrl(page)}${pos ? "@" + pos : ""}` state: { page, pos },
})
); );
window.dispatchEvent(new PopStateEvent("popstate"));
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
this.navigationResolve = resolve; this.navigationResolve = resolve;
}); });
@ -27,13 +27,16 @@ export class PathPageNavigator {
subscribe( subscribe(
pageLoadCallback: (pageName: string, pos: number) => Promise<void> pageLoadCallback: (pageName: string, pos: number) => Promise<void>
): void { ): void {
const cb = () => { const cb = (event?: PopStateEvent) => {
const gotoPage = this.getCurrentPage(); const gotoPage = this.getCurrentPage();
if (!gotoPage) { if (!gotoPage) {
return; return;
} }
safeRun(async () => { safeRun(async () => {
await pageLoadCallback(this.getCurrentPage(), this.getCurrentPos()); await pageLoadCallback(
this.getCurrentPage(),
event && event.state.pos
);
if (this.navigationResolve) { if (this.navigationResolve) {
this.navigationResolve(); this.navigationResolve();
} }