Skip to content
Open
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
48 changes: 9 additions & 39 deletions apps/obsidian/src/components/canvas/DiscourseRelationTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import { StateNode, TLEventHandlers, TLStateNodeConstructor } from "tldraw";
import { createShapeId } from "tldraw";
import type { TFile } from "obsidian";
import DiscourseGraphPlugin from "~/index";
import { getRelationTypeById } from "~/utils/typeUtils";
import { getNodeTypeById, getRelationTypeById } from "~/utils/typeUtils";
import {
getCompatibleTargetNodeTypeIds,
getDiscourseNodeTypeId,
} from "~/components/canvas/utils/relationTypeUtils";
import { DiscourseRelationShape } from "./shapes/DiscourseRelationShape";
import { getNodeTypeById } from "~/utils/typeUtils";
import { showToast } from "./utils/toastUtils";
import { toTldrawColor } from "~/utils/tldrawColors";

Expand Down Expand Up @@ -88,38 +91,6 @@ class Pointing extends StateNode {
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;

Expand All @@ -141,20 +112,19 @@ class Pointing extends StateNode {
return;
}

const sourceNodeTypeId = (target as { props?: { nodeTypeId?: string } })
.props?.nodeTypeId;
const sourceNodeTypeId = getDiscourseNodeTypeId(target);
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,
const compatibleTargetTypes = getCompatibleTargetNodeTypeIds({
discourseRelations: plugin.settings.discourseRelations,
relationTypeId,
sourceNodeTypeId,
);
});

if (compatibleTargetTypes.length === 0) {
const sourceNodeType = getNodeTypeById(plugin, sourceNodeTypeId);
Expand Down
6 changes: 5 additions & 1 deletion apps/obsidian/src/components/canvas/TldrawViewComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
} from "~/components/canvas/shapes/DiscourseRelationBinding";
import ToastListener from "./ToastListener";
import { RelationsOverlay } from "./overlays/RelationOverlay";
import { DragHandleOverlay } from "./overlays/DragHandleOverlay";
import { WHITE_LOGO_SVG } from "~/icons";
import { CustomContextMenu } from "./CustomContextMenu";
import {
Expand Down Expand Up @@ -474,7 +475,10 @@ export const TldrawPreviewComponent = ({
);
},
InFrontOfTheCanvas: () => (
<RelationsOverlay plugin={plugin} file={file} />
<>
<RelationsOverlay plugin={plugin} file={file} />
<DragHandleOverlay plugin={plugin} file={file} />
</>
),
}}
/>
Expand Down
Loading
Loading