Skip to content

Managed #181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Checkbox } from "./com/checkbox.tsx";
import { Page } from "./com/page.tsx";
import { TextField } from "./com/textfield.tsx";
import { Clock } from "./com/clock.tsx";
import { objectManaged } from "./model/hooks.ts";

export { BrowserBackend, SearchIndex_MiniSearch } from "./backend/browser.ts";
export { GitHubBackend } from "./backend/github.ts";
Expand Down Expand Up @@ -145,6 +146,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
if (!ctx.node) return;
if (ctx.node.childCount > 0) return;
if (ctx.node.componentCount > 0) return;
if (ctx.path.previous && objectManaged(ctx.path.previous)) return;
const path = ctx.path.clone();
path.pop(); // drop node
const field = workbench.workspace.new(ctx.node.name, "");
Expand Down Expand Up @@ -210,7 +212,11 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
if (ctx.node.raw.Rel === "Fields") return;
const node = ctx.node; // redraw seems to unset ctx.node
const path = ctx.path.clone();
const prev = node.prevSibling;
let prev = node.prevSibling;
while (prev && objectManaged(prev)) {
prev = prev.prevSibling;
if (!prev) return;
}
if (prev !== null) {
path.pop(); // drop node
path.push(prev);
Expand All @@ -229,6 +235,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
action: (ctx: Context) => {
if (!ctx.node) return;
if (ctx.node.raw.Rel === "Fields") return;
if (ctx.path.previous && objectManaged(ctx.path.previous)) return;
const node = ctx.node; // redraw seems to unset ctx.node
const parent = ctx.path.previous;
const path = ctx.path.clone();
Expand Down Expand Up @@ -263,7 +270,11 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
const p = ctx.path.clone();
p.pop(); // drop node
p.pop(); // drop parent
const parentSib = parent.prevSibling;
let parentSib = parent.prevSibling;
while (parentSib && objectManaged(parentSib)) {
parentSib = parentSib.prevSibling;
if (!parentSib) return;
}
p.push(parentSib);
p.push(node);
node.parent = parentSib;
Expand Down Expand Up @@ -299,7 +310,11 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
const p = ctx.path.clone();
p.pop(); // drop node
p.pop(); // drop parent
const parentSib = parent.nextSibling;
let parentSib = parent.nextSibling;
while (parentSib && objectManaged(parentSib)) {
parentSib = parentSib.nextSibling;
if (!parentSib) return;
}
p.push(parentSib);
p.push(node);
node.parent = parentSib;
Expand All @@ -323,6 +338,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Insert Child",
action: (ctx: Context, name: string = "", siblingIndex?: number) => {
if (!ctx.node) return;
if (objectManaged(ctx.node)) return;
const node = workbench.workspace.new(name);
node.parent = ctx.node;
if (siblingIndex !== undefined) {
Expand All @@ -338,6 +354,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Insert Before",
action: (ctx: Context) => {
if (!ctx.node) return;
if (ctx.path.previous && objectManaged(ctx.path.previous)) return;
const node = workbench.workspace.new("");
node.parent = ctx.node.parent;
node.siblingIndex = ctx.node.siblingIndex;
Expand All @@ -352,6 +369,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Insert Node",
action: (ctx: Context, name: string = "") => {
if (!ctx.node) return;
if (ctx.path.previous && objectManaged(ctx.path.previous)) return;
const node = workbench.workspace.new(name);
node.parent = ctx.node.parent;
node.siblingIndex = ctx.node.siblingIndex + 1;
Expand All @@ -368,6 +386,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
action: (ctx: Context) => {
// TODO: prevent creating reference to reference
if (!ctx.node) return;
if (ctx.path.previous && objectManaged(ctx.path.previous)) return;
const node = workbench.workspace.new("");
node.parent = ctx.node.parent;
node.siblingIndex = ctx.node.siblingIndex + 1;
Expand All @@ -384,6 +403,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
action: (ctx: Context) => {
if (!ctx.node) return;
if (ctx.node.id.startsWith("@")) return;
if (ctx.path.previous && objectManaged(ctx.path.previous)) return; // should probably provide feedback or disable delete
const above = workbench.workspace.findAbove(ctx.path);
ctx.node.destroy();
m.redraw.sync();
Expand Down
7 changes: 7 additions & 0 deletions lib/model/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ export function objectCall(obj: Node, hook: string, ...args: any[]): any {
return com.value[hook].apply(com.value, args);
}
}
}

// shorthand for nodes that have child provider hook.
// use this to determine if some commands should be
// prevented since visible children will not be impacted.
export function objectManaged(obj: Node): boolean {
return objectHas(obj, "objectChildren");
}