Skip to content

Commit

Permalink
tree undo
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 committed Aug 10, 2024
1 parent 1eb8fda commit fdc2ff7
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 11 deletions.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
"@node-projects/web-component-designer-widgets-wunderbaum/": "./node_modules/@node-projects/web-component-designer-widgets-wunderbaum/",
"@node-projects/web-component-designer-webrtc-multiplayer": "./node_modules/@node-projects/web-component-designer-webrtc-multiplayer/dist/index.js",
"@node-projects/web-component-designer-webrtc-multiplayer/": "./node_modules/@node-projects/web-component-designer-webrtc-multiplayer/",
"@node-projects/gitgraph-core/": "./node_modules/@node-projects/gitgraph-core/",
"@node-projects/gitgraph-core": "./node_modules/@node-projects/gitgraph-core/lib/index.js",
"@node-projects/gitgraph-js/": "./node_modules/@node-projects/gitgraph-js/",
"@node-projects/gitgraph-js": "./node_modules/@node-projects/gitgraph-js/lib/index.js",
"wunderbaum": "./node_modules/wunderbaum/dist/wunderbaum.esm.min.js",
"wunderbaum/": "./node_modules/wunderbaum/",
}
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"@doubletrade/lit-datepicker": "^1.0.0",
"@fortawesome/fontawesome-free": "^6.6.0",
"@node-projects/base-custom-webcomponent": "0.27.6",
"@node-projects/gitgraph-core": "^2.0.0",
"@node-projects/gitgraph-js": "^2.1.0",
"@node-projects/lean-he-esm": "^3.3.0",
"@node-projects/node-html-parser-esm": "^6.2.0",
"@node-projects/web-component-designer": "^0.1.204",
Expand Down
24 changes: 20 additions & 4 deletions src/CommandHandling.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { DocumentContainer, ServiceContainer } from '@node-projects/web-component-designer';
import { IUiCommandHandler } from '@node-projects/web-component-designer/dist/commandHandling/IUiCommandHandler';
import { DockManager } from 'dock-spawn-ts/lib/js/DockManager';
import { AppShell } from './appShell';
import { DockManager } from 'dock-spawn-ts/lib/js/DockManager.js';
import { AppShell } from './appShell.js';
import { WebRtcMultiplayerServer } from '@node-projects/web-component-designer-webrtc-multiplayer';
import { UndoRedoGraph } from './Undo_RedoGraph.js';

let multiplayer: WebRtcMultiplayerServer = null;;

Expand All @@ -16,8 +17,8 @@ export class CommandHandling {
this.init(serviceContainer);
}

handleCommandButtonClick(e) {
let button = e.currentTarget;
handleCommandButtonClick(e: MouseEvent) {
let button = <HTMLElement>e.currentTarget;
let commandName = button.dataset['command'];
let commandParameter = button.dataset['commandParameter'];

Expand All @@ -39,6 +40,21 @@ export class CommandHandling {
multiplayer.useBroadcast();
//multiplayer.startClient();
}
} else if (commandName === 'redo' && e.shiftKey) {
const target: DocumentContainer = <DocumentContainer>this.dockManager.activeDocument.resolvedElementContent;
if (target) {
const redos = Array.from(target.instanceServiceContainer.undoService.getRedoEntries());
let undoRedoGraph = new UndoRedoGraph(target.instanceServiceContainer.undoService);
undoRedoGraph.render(redos);
undoRedoGraph.style.left = e.x + 'px';
undoRedoGraph.style.top = e.y + 'px';
undoRedoGraph.style.width = 'auto';
undoRedoGraph.style.height = 'auto';
undoRedoGraph.style.zIndex = '9';
undoRedoGraph.style.position = 'absolute';
document.body.appendChild(undoRedoGraph);
}

} else if (this.dockManager.activeDocument) {
let target: any = this.dockManager.activeDocument.resolvedElementContent;
if (target.executeCommand) {
Expand Down
166 changes: 166 additions & 0 deletions src/Undo_RedoGraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
import { BaseCustomWebComponentConstructorAppend, html, css } from '@node-projects/base-custom-webcomponent';
import { ITransactionItem, IUndoService } from '@node-projects/web-component-designer';
import { Mode, Orientation, TemplateName, createGitgraph, templateExtend } from '@node-projects/gitgraph-js';
import { createG, createText, createPath, } from '@node-projects/gitgraph-js/lib/svg-elements.js';
import { BranchUserApi, Commit, GitgraphUserApi } from '@node-projects/gitgraph-core';

const PADDING = 10;
const OFFSET = 10;

export class UndoRedoGraph extends BaseCustomWebComponentConstructorAppend {

static readonly template = html`
<div id="root"></div>
`;

static readonly style = css`
:host {
box-sizing: border-box;
width: 100%;
height: 100%;
}
div {
width: 100%;
height: 100%;
background: black;
border: solid 1px white;
scale: 0.5;
transform-origin: 0 0;
}`;

static readonly is = 'undo-redo-graph';

static readonly properties = {

}

undoRedoService: IUndoService;

constructor(undoRedoService: IUndoService) {
super();
super._restoreCachedInititalValues();

this.undoRedoService = undoRedoService;
this.pointerDown = this.pointerDown.bind(this);
}

ready() {
this._parseAttributesToProperties();
this._assignEvents();
}

render(items: ITransactionItem[]) {
const graphContainer = this._getDomElement<HTMLDivElement>('root');


const withoutBranchLabels = templateExtend(TemplateName.Metro, {
branch: { label: { display: false } },
commit: {
message: {
displayHash: false,
}
}
});

const gitgraph = createGitgraph(graphContainer, { orientation: Orientation.Horizontal, mode: Mode.Linear, template: withoutBranchLabels });

const master = gitgraph.branch('' + this.#i++);
master.commit({ subject: 'start', renderTooltip: (c) => this.createTooltip(c) });

this.printItems(items, master, gitgraph, []);

window.addEventListener('pointerdown', this.pointerDown);
}

pointerDown(e: PointerEvent) {
let p = e.composedPath();
if (!p.includes(this)) {
window.removeEventListener('pointerdown', this.pointerDown);
this.remove();
}
}

#i = 0;

printItems(items: ITransactionItem[], branch: BranchUserApi<SVGElement>, gitgraph: GitgraphUserApi<SVGElement>, parent: ITransactionItem[]) {
const current= []
for (const i of items) {
let ak = [...parent,...current];
if (i.redoBranches) {
let a = [];
for (let ii of i.redoBranches) {
const f = gitgraph.branch('' + this.#i++);
a.push(() => this.printItems(ii.toReversed(), f, gitgraph, ak));
}
for (let u of a) {
u();
}
}
current.push(i);
ak = [...parent,...current];
branch.commit({
subject: i.title.replace("&lt;", "<").replace("&gt;", ">"),
renderTooltip: (c) => this.createTooltip(c),
onClick: () => {
console.log(ak);
//TODO
this.undoRedoService.redoTo(ak);
}
});

}
}

createTooltip(commit: Commit): SVGElement {
const path = createPath({ d: "", fill: "#EEE" });
const text = createText({
translate: { x: OFFSET + PADDING, y: 0 },
content: `${commit.subject}`,
fill: "#333",
});

const commitSize = commit.style.dot.size * 2;
const tooltip = createG({
translate: { x: commitSize, y: commitSize / 2 },
children: [path],
});

const observer = new MutationObserver(() => {
const { width } = text.getBBox();

const radius = 5;
const boxHeight = 50;
const boxWidth = OFFSET + width + 2 * PADDING;

const pathD = [
"M 0,0",
`L ${OFFSET},${OFFSET}`,
`V ${boxHeight / 2 - radius}`,
`Q ${OFFSET},${boxHeight / 2} ${OFFSET + radius},${boxHeight / 2}`,
`H ${boxWidth - radius}`,
`Q ${boxWidth},${boxHeight / 2} ${boxWidth},${boxHeight / 2 - radius}`,
`V -${boxHeight / 2 - radius}`,
`Q ${boxWidth},-${boxHeight / 2} ${boxWidth - radius},-${boxHeight / 2}`,
`H ${OFFSET + radius}`,
`Q ${OFFSET},-${boxHeight / 2} ${OFFSET},-${boxHeight / 2 - radius}`,
`V -${OFFSET}`,
"z",
].join(" ");

// Ideally, it would be great to refactor these behavior into SVG elements.
// rect.setAttribute("width", boxWidth.toString());
path.setAttribute("d", pathD.toString());
});

observer.observe(tooltip, {
attributes: false,
subtree: false,
childList: true,
});

tooltip.appendChild(text);

return tooltip;
}
}
customElements.define(UndoRedoGraph.is, UndoRedoGraph)
5 changes: 0 additions & 5 deletions src/UndoredoGraph.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/appShell.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NpmPackageLoader, BaseCustomWebcomponentBindingsService, JsonFileElementsService, DocumentContainer, CopyPasteAsJsonService, DebugView, UnkownElementsPropertiesService, sleep, RefactorView, BindingsRefactorService, TextRefactorService, SeperatorContextMenu, IDesignItem, DomConverter, PropertyGridWithHeader, DesignItem, ValueType } from '@node-projects/web-component-designer';
import { NpmPackageLoader, BaseCustomWebcomponentBindingsService, JsonFileElementsService, DocumentContainer, CopyPasteAsJsonService, DebugView, UnkownElementsPropertiesService, sleep, RefactorView, BindingsRefactorService, TextRefactorService, SeperatorContextMenu, IDesignItem, DomConverter, PropertyGridWithHeader, DesignItem, ValueType, UndoService } from '@node-projects/web-component-designer';
import createDefaultServiceContainer from '@node-projects/web-component-designer/dist/elements/services/DefaultServiceBootstrap.js';

import { NodeHtmlParserService } from '@node-projects/web-component-designer-htmlparserservice-nodehtmlparser';
Expand All @@ -19,6 +19,7 @@ serviceContainer.register("bindableObjectsService", new CustomBindableObjectsSer
serviceContainer.registerLast("propertyService", new UnkownElementsPropertiesService());
serviceContainer.register("refactorService", new BindingsRefactorService());
serviceContainer.register("refactorService", new TextRefactorService());
serviceContainer.register("undoService", (designerCanvas) => new UndoService(designerCanvas, true));

serviceContainer.config.codeViewWidget = CodeViewMonaco;

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
/* Basic Options */
"target": "ES2022", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"target": "ESNext", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "esnext", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
"allowJs": true, /* Allow javascript files to be compiled. */
Expand Down

0 comments on commit fdc2ff7

Please sign in to comment.