-
Notifications
You must be signed in to change notification settings - Fork 3
ENG-605: Add new relation flow #411
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
trangdoan982
merged 1 commit into
tldraw-obsidian
from
09-04-eng-605_add_new_relation_flow
Sep 25, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
344 changes: 344 additions & 0 deletions
344
apps/obsidian/src/components/canvas/DiscourseRelationTool.ts
This file contains hidden or 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,344 @@ | ||
| import { StateNode, TLEventHandlers, TLStateNodeConstructor } from "@tldraw/editor"; | ||
| import { createShapeId } from "tldraw"; | ||
| import type { TFile } from "obsidian"; | ||
| import DiscourseGraphPlugin from "~/index"; | ||
| import { getRelationTypeById } from "./utils/relationUtils"; | ||
| import { DiscourseRelationShape } from "./shapes/DiscourseRelationShape"; | ||
| import { getNodeTypeById } from "~/utils/utils"; | ||
| import { showToast } from "./utils/toastUtils"; | ||
|
|
||
| type RelationToolContext = { | ||
| plugin: DiscourseGraphPlugin; | ||
| canvasFile: TFile; | ||
| relationTypeId: string; | ||
| onRelationComplete?: () => void; | ||
| } | null; | ||
|
|
||
| let relationToolContext: RelationToolContext = null; | ||
|
|
||
| export const setDiscourseRelationToolContext = ( | ||
| args: RelationToolContext, | ||
| ): void => { | ||
| relationToolContext = args; | ||
| }; | ||
|
|
||
| export const clearDiscourseRelationToolContext = (): void => { | ||
| relationToolContext = null; | ||
| }; | ||
|
|
||
| export class DiscourseRelationTool extends StateNode { | ||
| static override id = "discourse-relation"; | ||
| static override initial = "idle"; | ||
| static override children = (): TLStateNodeConstructor[] => [Idle, Pointing]; | ||
|
|
||
| override onEnter = () => { | ||
| this.editor.setCursor({ type: "cross" }); | ||
| }; | ||
| } | ||
|
|
||
| class Idle extends StateNode { | ||
| static override id = "idle"; | ||
|
|
||
| override onPointerDown: TLEventHandlers["onPointerDown"] = (info) => { | ||
| this.parent.transition("pointing", info); | ||
| }; | ||
|
|
||
| override onEnter = () => { | ||
| this.editor.setCursor({ type: "cross", rotation: 0 }); | ||
| }; | ||
|
|
||
| override onCancel = () => { | ||
| this.editor.setCurrentTool("select"); | ||
| }; | ||
|
|
||
| override onKeyUp: TLEventHandlers["onKeyUp"] = (info) => { | ||
| if (info.key === "Enter") { | ||
| if (this.editor.getInstanceState().isReadonly) return null; | ||
| const onlySelectedShape = this.editor.getOnlySelectedShape(); | ||
| // If the only selected shape is editable, start editing it | ||
| if ( | ||
| onlySelectedShape && | ||
| this.editor.getShapeUtil(onlySelectedShape).canEdit(onlySelectedShape) | ||
| ) { | ||
| this.editor.setCurrentTool("select"); | ||
| this.editor.setEditingShape(onlySelectedShape.id); | ||
| this.editor.root.getCurrent()?.transition("editing_shape", { | ||
| ...info, | ||
| target: "shape", | ||
| shape: onlySelectedShape, | ||
| }); | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| class Pointing extends StateNode { | ||
| static override id = "pointing"; | ||
| shape?: DiscourseRelationShape; | ||
| markId = ""; | ||
|
|
||
| private showWarning = (message: string) => { | ||
| showToast({ | ||
| severity: "warning", | ||
| title: "Relation Tool", | ||
| description: message, | ||
| }); | ||
| this.cancel(); | ||
| }; | ||
|
|
||
| private getCompatibleNodeTypes = ( | ||
| plugin: DiscourseGraphPlugin, | ||
| relationTypeId: string, | ||
| sourceNodeTypeId: string, | ||
| ): string[] => { | ||
| const compatibleTypes: string[] = []; | ||
|
|
||
| // Find all discourse relations that match the relation type and source | ||
| const relations = plugin.settings.discourseRelations.filter( | ||
| (relation) => | ||
| relation.relationshipTypeId === relationTypeId && | ||
| relation.sourceId === sourceNodeTypeId, | ||
| ); | ||
|
|
||
| relations.forEach((relation) => { | ||
| compatibleTypes.push(relation.destinationId); | ||
| }); | ||
|
|
||
| // Also check reverse relations (where current node is destination) | ||
| const reverseRelations = plugin.settings.discourseRelations.filter( | ||
| (relation) => | ||
| relation.relationshipTypeId === relationTypeId && | ||
| relation.destinationId === sourceNodeTypeId, | ||
| ); | ||
|
|
||
| reverseRelations.forEach((relation) => { | ||
| compatibleTypes.push(relation.sourceId); | ||
| }); | ||
|
|
||
| return [...new Set(compatibleTypes)]; // Remove duplicates | ||
| }; | ||
|
|
||
| override onEnter = () => { | ||
| this.didTimeout = false; | ||
|
|
||
| const target = this.editor.getShapeAtPoint( | ||
| this.editor.inputs.currentPagePoint, | ||
| ); | ||
|
|
||
| if (!relationToolContext) { | ||
| this.showWarning("No relation type selected"); | ||
| return; | ||
| } | ||
|
|
||
| const plugin = relationToolContext.plugin; | ||
| const relationTypeId = relationToolContext.relationTypeId; | ||
|
|
||
| // Validate source node | ||
| if (!target || target.type !== "discourse-node") { | ||
| this.showWarning("Must start on a discourse node"); | ||
| return; | ||
| } | ||
|
|
||
| const sourceNodeTypeId = (target as { props?: { nodeTypeId?: string } }) | ||
| .props?.nodeTypeId; | ||
| if (!sourceNodeTypeId) { | ||
| this.showWarning("Source node must have a valid node type"); | ||
| return; | ||
| } | ||
|
|
||
| // Check if this source node type can create relations of this type | ||
| if (sourceNodeTypeId) { | ||
| const compatibleTargetTypes = this.getCompatibleNodeTypes( | ||
| plugin, | ||
| relationTypeId, | ||
| sourceNodeTypeId, | ||
| ); | ||
|
|
||
| if (compatibleTargetTypes.length === 0) { | ||
| const sourceNodeType = getNodeTypeById(plugin, sourceNodeTypeId); | ||
| const relationType = getRelationTypeById(plugin, relationTypeId); | ||
| this.showWarning( | ||
| `Node type "${sourceNodeType?.name}" cannot create "${relationType?.label}" relations`, | ||
| ); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| if (!target) { | ||
| this.createArrowShape(); | ||
| } else { | ||
| this.editor.setHintingShapes([target.id]); | ||
| } | ||
|
|
||
| this.startPreciseTimeout(); | ||
| }; | ||
|
|
||
| override onExit = () => { | ||
| this.shape = undefined; | ||
| this.editor.setHintingShapes([]); | ||
| this.clearPreciseTimeout(); | ||
| }; | ||
|
|
||
| override onPointerMove: TLEventHandlers["onPointerMove"] = () => { | ||
| if (this.editor.inputs.isDragging) { | ||
| if (!this.shape) { | ||
| this.createArrowShape(); | ||
| } | ||
|
|
||
| if (!this.shape) throw Error(`expected shape`); | ||
|
|
||
| this.updateArrowShapeEndHandle(); | ||
|
|
||
| this.editor.setCurrentTool("select.dragging_handle", { | ||
| shape: this.shape, | ||
| handle: { id: "end", type: "vertex", index: "a3", x: 0, y: 0 }, | ||
| isCreating: true, | ||
| onInteractionEnd: "select", | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| override onPointerUp: TLEventHandlers["onPointerUp"] = () => { | ||
| this.cancel(); | ||
| }; | ||
|
|
||
| override onCancel: TLEventHandlers["onCancel"] = () => { | ||
| this.cancel(); | ||
| }; | ||
|
|
||
| override onComplete: TLEventHandlers["onComplete"] = () => { | ||
| this.cancel(); | ||
| }; | ||
|
|
||
| override onInterrupt: TLEventHandlers["onInterrupt"] = () => { | ||
| this.cancel(); | ||
| }; | ||
|
|
||
| cancel() { | ||
| if (this.shape) { | ||
| // the arrow might not have been created yet! | ||
| this.editor.bailToMark(this.markId); | ||
| } | ||
| this.editor.setHintingShapes([]); | ||
| this.parent.transition("idle"); | ||
| } | ||
|
|
||
| createArrowShape() { | ||
| const { originPagePoint } = this.editor.inputs; | ||
|
|
||
| const id = createShapeId(); | ||
|
|
||
| this.markId = `creating:${id}`; | ||
| this.editor.mark(this.markId); | ||
|
|
||
| if (!relationToolContext) { | ||
| this.showWarning("Must start on a node"); | ||
| return; | ||
| } | ||
|
|
||
| const relationType = getRelationTypeById( | ||
| relationToolContext.plugin, | ||
| relationToolContext.relationTypeId, | ||
| ); | ||
|
|
||
| this.editor.createShape<DiscourseRelationShape>({ | ||
| id, | ||
| type: "discourse-relation", | ||
| x: originPagePoint.x, | ||
| y: originPagePoint.y, | ||
| props: { | ||
| relationTypeId: relationToolContext.relationTypeId, | ||
| text: relationType?.label ?? "", | ||
| scale: this.editor.user.getIsDynamicResizeMode() | ||
| ? 1 / this.editor.getZoomLevel() | ||
| : 1, | ||
| }, | ||
| }); | ||
|
|
||
| const shape = this.editor.getShape<DiscourseRelationShape>(id); | ||
| if (!shape) throw Error(`expected shape`); | ||
|
|
||
| const handles = this.editor.getShapeHandles(shape); | ||
| if (!handles) throw Error(`expected handles for arrow`); | ||
|
|
||
| const util = | ||
| this.editor.getShapeUtil<DiscourseRelationShape>("discourse-relation"); | ||
| const initial = this.shape; | ||
| const startHandle = handles.find((h) => h.id === "start")!; | ||
| const change = util.onHandleDrag?.(shape, { | ||
| handle: { ...startHandle, x: 0, y: 0 }, | ||
| isPrecise: true, | ||
| initial: initial, | ||
| }); | ||
|
|
||
| if (change) { | ||
| this.editor.updateShapes([change]); | ||
| } | ||
|
|
||
| // Cache the current shape after those changes | ||
| this.shape = this.editor.getShape(id); | ||
| this.editor.select(id); | ||
| } | ||
|
|
||
| updateArrowShapeEndHandle() { | ||
| const shape = this.shape; | ||
|
|
||
| if (!shape) throw Error(`expected shape`); | ||
|
|
||
| const handles = this.editor.getShapeHandles(shape); | ||
| if (!handles) throw Error(`expected handles for arrow`); | ||
|
|
||
| // start update | ||
| { | ||
| const util = | ||
| this.editor.getShapeUtil<DiscourseRelationShape>("discourse-relation"); | ||
| const initial = this.shape; | ||
| const startHandle = handles.find((h) => h.id === "start")!; | ||
| const change = util.onHandleDrag?.(shape, { | ||
| handle: { ...startHandle, x: 0, y: 0 }, | ||
| isPrecise: this.didTimeout, | ||
| initial: initial, | ||
| }); | ||
|
|
||
| if (change) { | ||
| this.editor.updateShapes([change]); | ||
| } | ||
| } | ||
|
|
||
| // end update | ||
| { | ||
| const util = | ||
| this.editor.getShapeUtil<DiscourseRelationShape>("discourse-relation"); | ||
| const initial = this.shape; | ||
| const point = this.editor.getPointInShapeSpace( | ||
| shape, | ||
| this.editor.inputs.currentPagePoint, | ||
| ); | ||
| const endHandle = handles.find((h) => h.id === "end")!; | ||
| const change = util.onHandleDrag?.(this.editor.getShape(shape)!, { | ||
| handle: { ...endHandle, x: point.x, y: point.y }, | ||
| isPrecise: false, | ||
| initial: initial, | ||
| }); | ||
|
|
||
| if (change) { | ||
| this.editor.updateShapes([change]); | ||
| } | ||
| } | ||
|
|
||
| // Cache the current shape after those changes | ||
| this.shape = this.editor.getShape(shape.id); | ||
| } | ||
|
|
||
| public preciseTimeout = -1; | ||
| public didTimeout = false; | ||
| public startPreciseTimeout() { | ||
| this.preciseTimeout = this.editor.timers.setTimeout(() => { | ||
| if (!this.getIsActive()) return; | ||
| this.didTimeout = true; | ||
| }, 320); | ||
| } | ||
| public clearPreciseTimeout() { | ||
| clearTimeout(this.preciseTimeout); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.