Skip to content

Commit

Permalink
lib: split out Workspace from Workbench. closes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Mar 14, 2023
1 parent 79f3b2d commit 2f1d1e3
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 161 deletions.
28 changes: 15 additions & 13 deletions lib/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Expand",
action: (ctx: Context) => {
if (!ctx.node) return;
workbench.setExpanded(ctx.panel.headNode, ctx.node, true);
workbench.workspace.setExpanded(ctx.panel.headNode, ctx.node, true);
m.redraw();
}
});
Expand All @@ -116,7 +116,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Collapse",
action: (ctx: Context) => {
if (!ctx.node) return;
workbench.setExpanded(ctx.panel.headNode, ctx.node, false);
workbench.workspace.setExpanded(ctx.panel.headNode, ctx.node, false);
m.redraw();
}
});
Expand All @@ -129,7 +129,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
const prev = ctx.node.getPrevSibling();
if (prev !== null) {
ctx.node.setParent(prev);
workbench.setExpanded(ctx.panel.headNode, prev, true);
workbench.workspace.setExpanded(ctx.panel.headNode, prev, true);

const node = ctx.node; // redraw seems to unset ctx.node
m.redraw.sync();
Expand All @@ -148,7 +148,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
ctx.node.setParent(parent.getParent());
ctx.node.setSiblingIndex(parent.getSiblingIndex()+1);
if (parent.childCount() === 0) {
workbench.setExpanded(ctx.panel.headNode, parent, false);
workbench.workspace.setExpanded(ctx.panel.headNode, parent, false);
}

const node = ctx.node; // redraw seems to unset ctx.node
Expand All @@ -174,7 +174,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
const parentSib = parent.getPrevSibling();
node.setParent(parentSib);
node.setSiblingIndex(parentSib.childCount()-1);
workbench.setExpanded(ctx.panel.headNode, parentSib, true);
workbench.workspace.setExpanded(ctx.panel.headNode, parentSib, true);
m.redraw.sync();
workbench.focus(node);
} else {
Expand Down Expand Up @@ -204,7 +204,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
const parentSib = parent.getNextSibling();
node.setParent(parentSib);
node.setSiblingIndex(0);
workbench.setExpanded(ctx.panel.headNode, parentSib, true);
workbench.workspace.setExpanded(ctx.panel.headNode, parentSib, true);
m.redraw.sync();
workbench.focus(node);
} else {
Expand All @@ -223,12 +223,12 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Insert Child",
action: (ctx: Context, name: string = "", siblingIndex?: number) => {
if (!ctx.node) return;
const node = workbench.nodes.new(name);
const node = workbench.workspace.new(name);
node.setParent(ctx.node);
if (siblingIndex !== undefined) {
node.setSiblingIndex(siblingIndex);
}
workbench.setExpanded(ctx.panel.headNode, ctx.node, true);
workbench.workspace.setExpanded(ctx.panel.headNode, ctx.node, true);
m.redraw.sync();
workbench.focus(node, ctx.panel, name.length);
}
Expand All @@ -238,7 +238,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Insert Before",
action: (ctx: Context) => {
if (!ctx.node) return;
const node = workbench.nodes.new("");
const node = workbench.workspace.new("");
node.setParent(ctx.node.getParent());
node.setSiblingIndex(ctx.node.getSiblingIndex());
m.redraw.sync();
Expand All @@ -250,7 +250,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
title: "Insert Node",
action: (ctx: Context, name: string = "") => {
if (!ctx.node) return;
const node = workbench.nodes.new(name);
const node = workbench.workspace.new(name);
node.setParent(ctx.node.getParent());
node.setSiblingIndex(ctx.node.getSiblingIndex()+1);
m.redraw.sync();
Expand Down Expand Up @@ -281,7 +281,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
id: "prev",
action: (ctx: Context) => {
if (!ctx.node) return;
const above = workbench.findAbove(ctx.panel.headNode, ctx.node);
const above = workbench.workspace.findAbove(ctx.panel.headNode, ctx.node);
if (above) {
workbench.focus(above, ctx.panel);
}
Expand All @@ -292,7 +292,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
id: "next",
action: (ctx: Context) => {
if (!ctx.node) return;
const below = workbench.findBelow(ctx.panel.headNode, ctx.node);
const below = workbench.workspace.findBelow(ctx.panel.headNode, ctx.node);
if (below) {
workbench.focus(below, ctx.panel);
}
Expand All @@ -311,6 +311,8 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
}
});
workbench.keybindings.registerBinding({command: "pick-command", key: "meta+k"});
workbench.keybindings.registerBinding({command: "pick-command", key: "meta+j"});
workbench.keybindings.registerBinding({command: "pick-command", key: "shift+ctrl+k"});
workbench.commands.registerCommand({
id: "new-panel",
title: "Open in New Panel",
Expand Down Expand Up @@ -342,7 +344,7 @@ export async function setup(document: Document, target: HTMLElement, backend: Ba
action: (ctx: Context) => {
if (!ctx.node) return;
[...Array(100)].forEach(() => {
const node = workbench.nodes.new(generateName(8));
const node = workbench.workspace.new(generateName(8));
node.setParent(ctx.node);
});
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const App: m.Component = {
<div style={{display: "flex", flexGrow: "1"}}>
{state.open &&
<div style={{width: "200px", padding: "var(--padding)"}}>
{workbench.nodes.getRoot().getChildren().map(node => <NavNode node={node} expanded={true} level={0} workbench={workbench} />)}
{workbench.workspace.module.getRoot().getChildren().map(node => <NavNode node={node} expanded={true} level={0} workbench={workbench} />)}
</div>
}
<div style={{flexGrow: "1", borderLeft: "1px solid var(--dark)"}}>
Expand Down
10 changes: 5 additions & 5 deletions lib/ui/outline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface State {
export const OutlineNode: m.Component<Attrs, State> = {
view ({attrs, state, children}) {
const {node, panel, workbench} = attrs;
const expanded = workbench.getExpanded(panel.headNode, node);
const expanded = workbench.workspace.getExpanded(panel.headNode, node);
const hover = (e) => {
state.hover = true;
e.stopPropagation();
Expand Down Expand Up @@ -65,7 +65,7 @@ export const OutlineNode: m.Component<Attrs, State> = {
e.stopPropagation();

// TODO: make this work as a command?
const prev = workbench.findAbove(panel.headNode, node);
const prev = workbench.workspace.findAbove(panel.headNode, node);
if (!prev) {
return;
}
Expand All @@ -83,7 +83,7 @@ export const OutlineNode: m.Component<Attrs, State> = {
if (e.ctrlKey || e.shiftKey || e.metaKey || e.altKey) return;
// cursor at end of text
if (e.target.selectionStart === e.target.value.length) {
if (node.childCount() > 0 && workbench.getExpanded(panel.headNode, node)) {
if (node.childCount() > 0 && workbench.workspace.getExpanded(panel.headNode, node)) {
workbench.executeCommand("insert-child", {node, panel}, "", 0);
} else {
workbench.executeCommand("insert", {node, panel});
Expand Down Expand Up @@ -183,7 +183,7 @@ export const OutlineNode: m.Component<Attrs, State> = {
<div style={{flexGrow: "1"}}>
{(node.childCount() > 0)
?node.getChildren().map(n => <OutlineNode key={n.ID} workbench={workbench} panel={panel} node={n} />)
:<NewNode workbench={workbench} node={node} />
:<NewNode workbench={workbench} panel={panel} node={node} />
}
</div>
</div>
Expand All @@ -204,7 +204,7 @@ export const NewNode = {
e.preventDefault();
if (node.childCount() > 0) {
const lastchild = node.getChildren()[node.childCount()-1];
workbench.executeCommand("insert-child", {node: lastchild, panel: panel});
workbench.executeCommand("insert-child", {node: lastchild, panel});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const Search: m.Component = {

if (state.query) {
state.results = workbench.backend.index.search(state.query).map(id => {
let node = workbench.nodes.find(id);
let node = workbench.workspace.find(id);
if (!node) {
return undefined;
}
Expand Down
Loading

0 comments on commit 2f1d1e3

Please # to comment.