Fix some nasty bug
This commit is contained in:
parent
4d201efdbb
commit
b858917c98
@ -37,9 +37,8 @@ self.syscall = async (name: string, ...args: any[]) => {
|
||||
});
|
||||
};
|
||||
|
||||
function wrapScript(code: string): string {
|
||||
return `const fn = ${code};
|
||||
return fn["default"].apply(null, arguments);`;
|
||||
function wrapScript(code: string) {
|
||||
return `return (${code})["default"]`;
|
||||
}
|
||||
|
||||
self.addEventListener("message", (event: { data: WorkerMessage }) => {
|
||||
@ -47,7 +46,8 @@ self.addEventListener("message", (event: { data: WorkerMessage }) => {
|
||||
let data = event.data;
|
||||
switch (data.type) {
|
||||
case "load":
|
||||
loadedFunctions.set(data.name!, new Function(wrapScript(data.code!)));
|
||||
let fn2 = new Function(wrapScript(data.code!));
|
||||
loadedFunctions.set(data.name!, fn2());
|
||||
postMessage(
|
||||
{
|
||||
type: "inited",
|
||||
|
@ -79,8 +79,8 @@ export async function updateMaterializedQueriesOnPage(pageName: string) {
|
||||
for (let {
|
||||
key,
|
||||
page,
|
||||
value: {item, children},
|
||||
} of await syscall("index.scanPrefixGlobal", "it:")) {
|
||||
value: { item, children },
|
||||
}; of await syscall("index.scanPrefixGlobal", "it:")) {
|
||||
let [, pos] = key.split(":");
|
||||
if (!filter || (filter && item.includes(filter))) {
|
||||
results.push(`* [[${page}@${pos}]] ${item}`);
|
||||
|
1
plugs/emoji/.gitignore
vendored
Normal file
1
plugs/emoji/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
emoji-data.txt
|
3
plugs/emoji/Makefile
Normal file
3
plugs/emoji/Makefile
Normal 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
17
plugs/emoji/build.js
Normal 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));
|
4
plugs/emoji/emoji.plug.yaml
Normal file
4
plugs/emoji/emoji.plug.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
functions:
|
||||
emojiCompleter:
|
||||
path: "./emoji.ts:emojiCompleter"
|
||||
isCompleter: true
|
26
plugs/emoji/emoji.ts
Normal file
26
plugs/emoji/emoji.ts
Normal 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",
|
||||
})),
|
||||
};
|
||||
}
|
@ -86,7 +86,7 @@ export class ExpressServer {
|
||||
res.status(200);
|
||||
res.send("");
|
||||
}
|
||||
};)
|
||||
})
|
||||
.put(bodyParser.text({ type: "*/*" }), async (req, res) => {
|
||||
let pageName = req.params[0];
|
||||
console.log("Saving", pageName);
|
||||
|
@ -18,7 +18,7 @@ let args = yargs(hideBin(process.argv))
|
||||
|
||||
if (!args._.length) {
|
||||
console.error("Usage: silverbullet <path-to-pages>");
|
||||
process.exit(1);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const pagesPath = args._[0] as string;
|
||||
|
@ -51,6 +51,13 @@ function fuzzyFilter(pattern: string, options: Option[]): Option[] {
|
||||
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({
|
||||
placeholder,
|
||||
options,
|
||||
@ -59,6 +66,7 @@ export function FilterList({
|
||||
onKeyPress,
|
||||
allowNew = false,
|
||||
helpText = "",
|
||||
completePrefix,
|
||||
icon,
|
||||
newHint,
|
||||
}: {
|
||||
@ -68,6 +76,7 @@ export function FilterList({
|
||||
onKeyPress?: (key: string, currentText: string) => void;
|
||||
onSelect: (option: Option | undefined) => void;
|
||||
allowNew?: boolean;
|
||||
completePrefix?: string;
|
||||
helpText: string;
|
||||
newHint?: string;
|
||||
icon?: IconDefinition;
|
||||
@ -90,7 +99,7 @@ export function FilterList({
|
||||
|
||||
if (searchPhrase) {
|
||||
let foundExactMatch = false;
|
||||
let results = fuzzyFilter(searchPhrase, options);
|
||||
let results = simpleFilter(searchPhrase, options);
|
||||
results = results.sort(magicSorter);
|
||||
if (allowNew && !foundExactMatch) {
|
||||
results.push({
|
||||
@ -139,7 +148,7 @@ export function FilterList({
|
||||
ref={searchBoxRef}
|
||||
onChange={filterUpdate}
|
||||
onKeyDown={(e: React.KeyboardEvent) => {
|
||||
// console.log("Key up", e.key);
|
||||
// console.log("Key up", e);
|
||||
if (onKeyPress) {
|
||||
onKeyPress(e.key, text);
|
||||
}
|
||||
@ -159,6 +168,12 @@ export function FilterList({
|
||||
case "Escape":
|
||||
onSelect(undefined);
|
||||
break;
|
||||
case " ":
|
||||
if (completePrefix) {
|
||||
setText(completePrefix);
|
||||
e.preventDefault();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}}
|
||||
/>
|
||||
|
@ -27,6 +27,11 @@ export function PageNavigator({
|
||||
orderId: orderId,
|
||||
});
|
||||
}
|
||||
let completePrefix: string | undefined = undefined;
|
||||
if (currentPage && currentPage.includes("/")) {
|
||||
const pieces = currentPage.split("/");
|
||||
completePrefix = pieces.slice(0, pieces.length - 1).join("/") + "/";
|
||||
}
|
||||
return (
|
||||
<FilterList
|
||||
placeholder="Page"
|
||||
@ -36,6 +41,7 @@ export function PageNavigator({
|
||||
allowNew={true}
|
||||
helpText="Start typing the page name to filter results, press <code>Return</code> to open."
|
||||
newHint="Create page"
|
||||
completePrefix={completePrefix}
|
||||
onSelect={(opt) => {
|
||||
onNavigate(opt?.name);
|
||||
}}
|
||||
|
@ -17,23 +17,26 @@ export class CompleterHook implements Hook<CompleterHookT> {
|
||||
continue;
|
||||
}
|
||||
for (const [functionName, functionDef] of Object.entries(
|
||||
plug.manifest.functions
|
||||
plug.manifest.functions
|
||||
)) {
|
||||
if (functionDef.isCompleter) {
|
||||
completerPromises.push(plug.invoke(functionName, []));
|
||||
}
|
||||
}
|
||||
}
|
||||
let allCompletionResults = await Promise.all(completerPromises);
|
||||
if (allCompletionResults.length === 1) {
|
||||
return allCompletionResults[0];
|
||||
} else if (allCompletionResults.length > 1) {
|
||||
console.error(
|
||||
"Got completion results from multiple sources, cannot deal with that",
|
||||
allCompletionResults
|
||||
);
|
||||
let actualResult = null;
|
||||
for (const result of await Promise.all(completerPromises)) {
|
||||
if (result) {
|
||||
if (actualResult) {
|
||||
console.error(
|
||||
"Got completion results from multiple sources, cannot deal with that"
|
||||
);
|
||||
return null;
|
||||
}
|
||||
actualResult = result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
return actualResult;
|
||||
}
|
||||
|
||||
apply(system: System<CompleterHookT>): void {
|
||||
|
@ -12,12 +12,12 @@ export class PathPageNavigator {
|
||||
navigationResolve?: () => void;
|
||||
|
||||
async navigate(page: string, pos?: number) {
|
||||
window.history.pushState(
|
||||
{ page, pos },
|
||||
page,
|
||||
`/${encodePageUrl(page)}${pos ? "@" + pos : ""}`
|
||||
window.history.pushState({ page, pos }, page, `/${encodePageUrl(page)}`);
|
||||
window.dispatchEvent(
|
||||
new PopStateEvent("popstate", {
|
||||
state: { page, pos },
|
||||
})
|
||||
);
|
||||
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||
await new Promise<void>((resolve) => {
|
||||
this.navigationResolve = resolve;
|
||||
});
|
||||
@ -27,19 +27,22 @@ export class PathPageNavigator {
|
||||
subscribe(
|
||||
pageLoadCallback: (pageName: string, pos: number) => Promise<void>
|
||||
): void {
|
||||
const cb = () => {
|
||||
const gotoPage = this.getCurrentPage();
|
||||
if (!gotoPage) {
|
||||
return;
|
||||
}
|
||||
safeRun(async () => {
|
||||
await pageLoadCallback(this.getCurrentPage(), this.getCurrentPos());
|
||||
if (this.navigationResolve) {
|
||||
this.navigationResolve();
|
||||
const cb = (event?: PopStateEvent) => {
|
||||
const gotoPage = this.getCurrentPage();
|
||||
if (!gotoPage) {
|
||||
return;
|
||||
}
|
||||
});
|
||||
};
|
||||
window.addEventListener("popstate", cb);
|
||||
safeRun(async () => {
|
||||
await pageLoadCallback(
|
||||
this.getCurrentPage(),
|
||||
event && event.state.pos
|
||||
);
|
||||
if (this.navigationResolve) {
|
||||
this.navigationResolve();
|
||||
}
|
||||
});
|
||||
};
|
||||
window.addEventListener("popstate", cb);
|
||||
cb();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user