Refactor, cleanup

This commit is contained in:
Zef Hemel
2025-06-12 20:30:01 +02:00
parent b942ebabad
commit 8d3dc4cc4e
120 changed files with 1139 additions and 1101 deletions
+1
View File
@@ -2,6 +2,7 @@ export const activeWidgets = new Set<DomWidget>();
export interface DomWidget {
dom?: HTMLElement;
renderContent(
div: HTMLElement,
cachedHtml: string | undefined,
+6 -6
View File
@@ -19,6 +19,12 @@ export class IFrameWidget extends WidgetType {
super();
}
override get estimatedHeight(): number {
const cachedHeight = this.client.getCachedWidgetHeight(this.bodyText);
// console.log("Calling estimated height", this.bodyText, cachedHeight);
return cachedHeight > 0 ? cachedHeight : 150;
}
toDOM(): HTMLElement {
const from = this.from;
const iframe = createWidgetSandboxIFrame(
@@ -67,12 +73,6 @@ export class IFrameWidget extends WidgetType {
return iframe;
}
override get estimatedHeight(): number {
const cachedHeight = this.client.getCachedWidgetHeight(this.bodyText);
// console.log("Calling estimated height", this.bodyText, cachedHeight);
return cachedHeight > 0 ? cachedHeight : 150;
}
override eq(other: WidgetType): boolean {
return (
other instanceof IFrameWidget &&
+5 -5
View File
@@ -31,11 +31,6 @@ class InlineContentWidget extends WidgetType {
super();
}
override eq(other: InlineContentWidget) {
return other.url === this.url && other.title === this.title &&
JSON.stringify(other.dim) === JSON.stringify(this.dim);
}
override get estimatedHeight(): number {
const cachedHeight = this.client.getCachedWidgetHeight(
`content:${this.url}`,
@@ -43,6 +38,11 @@ class InlineContentWidget extends WidgetType {
return cachedHeight;
}
override eq(other: InlineContentWidget) {
return other.url === this.url && other.title === this.title &&
JSON.stringify(other.dim) === JSON.stringify(this.dim);
}
toDOM() {
const div = document.createElement("div");
div.className = "sb-inline-content";
+1
View File
@@ -52,6 +52,7 @@ class ListBulletWidget extends WidgetType {
constructor(readonly bullet: string) {
super();
}
toDOM(): HTMLElement {
const listBullet = document.createElement("span");
listBullet.textContent = this.bullet;
+8 -5
View File
@@ -7,16 +7,19 @@ import {
isCursorInRange,
} from "./util.ts";
import type { Client } from "../client.ts";
import { parse as parseLua } from "../space_lua/parse.ts";
import type { LuaBlock, LuaFunctionCallStatement } from "../space_lua/ast.ts";
import { evalExpression } from "../space_lua/eval.ts";
import { parse as parseLua } from "../../lib/space_lua/parse.ts";
import type {
LuaBlock,
LuaFunctionCallStatement,
} from "../../lib/space_lua/ast.ts";
import { evalExpression } from "../../lib/space_lua/eval.ts";
import {
LuaEnv,
LuaRuntimeError,
LuaStackFrame,
luaValueToJS,
singleResult,
} from "../space_lua/runtime.ts";
import { LuaRuntimeError } from "../space_lua/runtime.ts";
} from "../../lib/space_lua/runtime.ts";
import { encodeRef } from "@silverbulletmd/silverbullet/lib/page_ref";
import { resolveASTReference } from "../space_lua.ts";
import { LuaWidget } from "./lua_widget.ts";
+6 -6
View File
@@ -13,7 +13,7 @@ import {
moveCursorIntoText,
} from "./widget_util.ts";
import { expandMarkdown } from "../markdown.ts";
import { LuaStackFrame, LuaTable } from "../space_lua/runtime.ts";
import { LuaStackFrame, LuaTable } from "../../lib/space_lua/runtime.ts";
import { isBlockMarkdown, jsonToMDTable } from "../markdown_util.ts";
import { activeWidgets } from "./code_widget.ts";
@@ -55,6 +55,11 @@ export class LuaWidget extends WidgetType {
super();
}
override get estimatedHeight(): number {
const cacheItem = this.client.getWidgetCache(this.cacheKey);
return cacheItem ? cacheItem.height : -1;
}
toDOM(): HTMLElement {
const wrapperSpan = document.createElement("span");
wrapperSpan.className = "sb-lua-wrapper";
@@ -260,11 +265,6 @@ export class LuaWidget extends WidgetType {
return container;
}
override get estimatedHeight(): number {
const cacheItem = this.client.getWidgetCache(this.cacheKey);
return cacheItem ? cacheItem.height : -1;
}
override eq(other: WidgetType): boolean {
return (
other instanceof LuaWidget &&
+10 -9
View File
@@ -16,10 +16,11 @@ import {
resolvePath,
} from "@silverbulletmd/silverbullet/lib/resolve";
import { expandMarkdown } from "../markdown.ts";
import { LuaStackFrame } from "../space_lua/runtime.ts";
import { LuaStackFrame } from "../../lib/space_lua/runtime.ts";
class TableViewWidget extends WidgetType {
tableBodyText: string;
constructor(
readonly pos: number,
readonly client: Client,
@@ -29,6 +30,14 @@ class TableViewWidget extends WidgetType {
this.tableBodyText = renderToText(t);
}
override get estimatedHeight(): number {
const height = this.client.getCachedWidgetHeight(
`table:${this.tableBodyText}`,
);
// console.log("Calling estimated height for table", height);
return height;
}
toDOM(): HTMLElement {
const dom = document.createElement("span");
dom.classList.add("sb-table-widget");
@@ -76,14 +85,6 @@ class TableViewWidget extends WidgetType {
return dom;
}
override get estimatedHeight(): number {
const height = this.client.getCachedWidgetHeight(
`table:${this.tableBodyText}`,
);
// console.log("Calling estimated height for table", height);
return height;
}
override eq(other: WidgetType): boolean {
return (
other instanceof TableViewWidget &&
+1
View File
@@ -14,6 +14,7 @@ class CheckboxWidget extends WidgetType {
) {
super();
}
toDOM(): HTMLElement {
const wrap = document.createElement("span");
wrap.classList.add("sb-checkbox");
+3
View File
@@ -26,6 +26,7 @@ export class LinkWidget extends WidgetType {
) {
super();
}
toDOM(): HTMLElement {
const anchor = document.createElement("a");
anchor.className = this.options.cssClass;
@@ -80,6 +81,7 @@ export class HtmlWidget extends WidgetType {
) {
super();
}
toDOM(): HTMLElement {
const el = document.createElement("span");
if (this.className) {
@@ -119,6 +121,7 @@ export class ButtonWidget extends WidgetType {
) {
super();
}
toDOM(): HTMLElement {
const anchor = document.createElement("button");
anchor.className = this.cssClass;