-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added canvas context menu and node/link appearance animation
- Loading branch information
Showing
8 changed files
with
368 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Point } from "./Point"; | ||
|
||
export class CanvasContextMenu extends EventTarget | ||
{ | ||
public static readonly createNodeOptionClickedEvent = "create-node-option-clicked"; | ||
public static readonly lockCanvasSwitchClickedEvent = "lock-canvas-switch-clicked"; | ||
|
||
public constructor() | ||
{ | ||
super(); | ||
|
||
this._menuContainer.addEventListener("mousedown", () => | ||
{ | ||
this.closeMenu(); | ||
}); | ||
|
||
this.setupMenuOption(this._createNodeOption, CanvasContextMenu.createNodeOptionClickedEvent); | ||
this.setupMenuOption(this._lockCanvasSwitch, CanvasContextMenu.lockCanvasSwitchClickedEvent); | ||
} | ||
|
||
public addMenuTo(node: HTMLElement | SVGElement): void | ||
{ | ||
let canvasContextMenu = | ||
document.querySelector("#canvas-context-menu-container>.context-menu") as HTMLDivElement; | ||
|
||
node.addEventListener("contextmenu", (event) => | ||
{ | ||
let mouseEvent = event as MouseEvent; | ||
|
||
event.preventDefault(); | ||
|
||
this._openingPosition = { x: mouseEvent.clientX, y: mouseEvent.clientY }; | ||
|
||
canvasContextMenu.style.top = `${mouseEvent.pageY + 5}px`; | ||
canvasContextMenu.style.left = `${mouseEvent.pageX + 5}px`; | ||
|
||
this.openMenu(); | ||
}); | ||
} | ||
|
||
public closeMenu(): void | ||
{ | ||
this._isMenuOpened = false; | ||
|
||
this._menuContainer.classList.add("hidden"); | ||
} | ||
|
||
public openMenu(): void | ||
{ | ||
this._isMenuOpened = true; | ||
|
||
this._menuContainer.classList.remove("hidden"); | ||
} | ||
|
||
public get isMenuOpened(): boolean | ||
{ | ||
return this._isMenuOpened; | ||
} | ||
|
||
public get openingPosition(): Point | undefined | ||
{ | ||
return this._openingPosition; | ||
} | ||
|
||
public setCanvasLockedSwitchEnabled(canvasLocked: boolean) | ||
{ | ||
if (canvasLocked) | ||
{ | ||
this._lockCanvasSwitch.classList.add("enabled"); | ||
} | ||
else | ||
{ | ||
this._lockCanvasSwitch.classList.remove("enabled"); | ||
} | ||
} | ||
|
||
private setupMenuOption(optionNode: HTMLElement, eventName: string) | ||
{ | ||
optionNode.addEventListener("mousedown", (event) => | ||
{ | ||
event.stopPropagation(); | ||
}); | ||
|
||
optionNode.addEventListener("click", () => | ||
{ | ||
this.dispatchEvent(new Event(eventName)); | ||
|
||
this._menuContainer.classList.add("hidden"); | ||
this._isMenuOpened = false; | ||
}); | ||
} | ||
|
||
private _menuContainer = document.querySelector("#canvas-context-menu-container") as HTMLDivElement; | ||
private _lockCanvasSwitch = document.querySelector("#lock-canvas-switch") as HTMLDivElement; | ||
private _createNodeOption = document.querySelector("#create-node-option") as HTMLDivElement; | ||
|
||
private _isMenuOpened = false; | ||
private _openingPosition?: Point; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { PanZoom } from "panzoom"; | ||
|
||
export class Settings extends EventTarget | ||
{ | ||
public static readonly isCanvasLockedChangedEvent = "canvas-locked-changed"; | ||
|
||
public static get instance() | ||
{ | ||
return this._instance; | ||
} | ||
|
||
public setPanContext(panContext: PanZoom): void | ||
{ | ||
this._panContext = panContext; | ||
} | ||
|
||
public get isCanvasLocked(): boolean | ||
{ | ||
return this._isCanvasLocked; | ||
} | ||
|
||
public set isCanvasLocked(canvasLocked: boolean) | ||
{ | ||
if (this._panContext == undefined) | ||
{ | ||
throw Error("Pan context must be initialized before locking canvas"); | ||
} | ||
|
||
if (canvasLocked) | ||
{ | ||
this._panContext.pause(); | ||
} | ||
else | ||
{ | ||
this._panContext.resume(); | ||
} | ||
|
||
this._isCanvasLocked = canvasLocked; | ||
|
||
this.dispatchEvent(new Event(Settings.isCanvasLockedChangedEvent)); | ||
} | ||
|
||
private constructor() | ||
{ | ||
super(); | ||
} | ||
|
||
private static readonly _instance = new Settings(); | ||
|
||
private _panContext?: PanZoom; | ||
|
||
private _isCanvasLocked = false; | ||
} |
Oops, something went wrong.