Skip to content
Merged
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
31 changes: 30 additions & 1 deletion apps/obsidian/src/components/ModifyNodeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ type ModifyNodeFormProps = {
/** DiscourseRelation.id; when set, relation is created with currentFile as the other end. */
relationshipId?: string;
relationshipTargetFile?: TFile;
insertBacklink: boolean;
}) => Promise<void>;
onCancel: () => void;
initialTitle?: string;
initialNodeType?: DiscourseNode;
initialFile?: TFile; // for edit mode
currentFile?: TFile; // the file where the node is being created from
plugin: DiscourseGraphPlugin;
/** When true, show the insert-backlink checkbox (editor flows that honor insertBacklink). */
showInsertBacklinkOption?: boolean;
};

export const ModifyNodeForm = ({
Expand All @@ -67,6 +70,7 @@ export const ModifyNodeForm = ({
initialFile,
currentFile,
plugin,
showInsertBacklinkOption = false,
}: ModifyNodeFormProps) => {
const isEditMode = !!initialFile;
const [title, setTitle] = useState(initialFile?.basename || initialTitle);
Expand All @@ -83,6 +87,7 @@ export const ModifyNodeForm = ({
const [selectedRelationshipKey, setSelectedRelationshipKey] = useState<
string | undefined
>(undefined);
const [insertBacklink, setInsertBacklink] = useState(!!initialTitle);
const queryEngine = useRef(new QueryEngine(plugin.app));
const titleInputRef = useRef<HTMLTextAreaElement>(null);
const popoverRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -278,6 +283,7 @@ export const ModifyNodeForm = ({
setSelectedExistingNode(file);
setQuery(file.basename);
setTitle(file.basename);
setInsertBacklink(true);
// Auto-detect node type from the selected file's frontmatter
const nodeTypeId = await getNodeTypeIdForFile(plugin, file);
if (nodeTypeId && selectedFileRef.current === file) {
Expand All @@ -291,12 +297,13 @@ export const ModifyNodeForm = ({
const handleClearSelection = useCallback(() => {
selectedFileRef.current = null;
setSelectedExistingNode(null);
setInsertBacklink(!!initialTitle);
setQuery("");
setTitle("");
setTimeout(() => {
titleInputRef.current?.focus();
}, 50);
}, []);
}, [initialTitle]);

const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (selectedExistingNode) {
Expand Down Expand Up @@ -391,6 +398,7 @@ export const ModifyNodeForm = ({
selectedExistingNode: selectedExistingNode || undefined,
relationshipId: selectedRel?.uniqueKey || undefined,
relationshipTargetFile: currentFile || undefined,
insertBacklink,
});
onCancel();
} catch (error) {
Expand Down Expand Up @@ -418,6 +426,7 @@ export const ModifyNodeForm = ({
selectedRelationshipKey,
currentFile,
availableRelationships,
insertBacklink,
]);

return (
Expand Down Expand Up @@ -568,6 +577,20 @@ export const ModifyNodeForm = ({
</div>
)}

{!isEditMode && showInsertBacklinkOption && (
<div className="setting-item">
<div className="setting-item-name">Insert backlink</div>
<div className="setting-item-control">
<input
type="checkbox"
checked={insertBacklink}
onChange={(e) => setInsertBacklink(e.target.checked)}
disabled={isSubmitting}
/>
</div>
</div>
)}

<div className="modal-button-container mt-5 flex justify-end gap-2">
<button
type="button"
Expand Down Expand Up @@ -606,11 +629,13 @@ type ModifyNodeModalProps = {
selectedExistingNode?: TFile;
relationshipId?: string;
relationshipTargetFile?: TFile;
insertBacklink: boolean;
}) => Promise<void>;
initialTitle?: string;
initialNodeType?: DiscourseNode;
initialFile?: TFile;
currentFile?: TFile;
showInsertBacklinkOption?: boolean;
};

class ModifyNodeModal extends Modal {
Expand All @@ -622,13 +647,15 @@ class ModifyNodeModal extends Modal {
selectedExistingNode?: TFile;
relationshipId?: string;
relationshipTargetFile?: TFile;
insertBacklink: boolean;
}) => Promise<void>;
private root: Root | null = null;
private initialTitle?: string;
private initialNodeType?: DiscourseNode;
private initialFile?: TFile;
private currentFile?: TFile;
private plugin: DiscourseGraphPlugin;
private showInsertBacklinkOption?: boolean;

constructor(app: App, props: ModifyNodeModalProps) {
super(app);
Expand All @@ -639,6 +666,7 @@ class ModifyNodeModal extends Modal {
this.initialFile = props.initialFile;
this.currentFile = props.currentFile;
this.plugin = props.plugin;
this.showInsertBacklinkOption = props.showInsertBacklinkOption;
}

onOpen() {
Expand All @@ -657,6 +685,7 @@ class ModifyNodeModal extends Modal {
initialFile={this.initialFile}
currentFile={this.currentFile}
plugin={this.plugin}
showInsertBacklinkOption={this.showInsertBacklinkOption}
/>
</StrictMode>,
);
Expand Down
1 change: 1 addition & 0 deletions apps/obsidian/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export default class DiscourseGraphPlugin extends Plugin {
initialTitle: selection,
initialNodeType: nodeType,
currentFile,
showInsertBacklinkOption: true,
onSubmit: createModifyNodeModalSubmitHandler(this, editor),
}).open();
},
Expand Down
2 changes: 1 addition & 1 deletion apps/obsidian/src/utils/createNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export const createDiscourseNode = async ({
nodeType,
});

if (newFile && editor && editor.somethingSelected()) {
if (newFile && editor) {
editor.replaceSelection(`[[${formattedNodeName}]]`);
}

Expand Down
10 changes: 7 additions & 3 deletions apps/obsidian/src/utils/registerCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type ModifyNodeSubmitParams = {
selectedExistingNode?: TFile;
relationshipId?: string;
relationshipTargetFile?: TFile;
insertBacklink: boolean;
};

export const createModifyNodeModalSubmitHandler = (
Expand All @@ -34,10 +35,11 @@ export const createModifyNodeModalSubmitHandler = (
selectedExistingNode,
relationshipId,
relationshipTargetFile,
insertBacklink,
}: ModifyNodeSubmitParams) => {
if (selectedExistingNode) {
if (editor && editor.somethingSelected()) {
editor?.replaceSelection(`[[${selectedExistingNode.basename}]]`);
if (insertBacklink && editor) {
editor.replaceSelection(`[[${selectedExistingNode.basename}]]`);
}
await addRelationIfRequested(plugin, selectedExistingNode, {
relationshipId,
Expand All @@ -48,7 +50,7 @@ export const createModifyNodeModalSubmitHandler = (
plugin,
nodeType,
text: title,
editor,
editor: insertBacklink ? editor : undefined,
});
if (newFile) {
await addRelationIfRequested(plugin, newFile, {
Expand Down Expand Up @@ -84,6 +86,7 @@ export const registerCommands = (plugin: DiscourseGraphPlugin) => {
nodeTypes: plugin.settings.nodeTypes,
plugin,
currentFile,
showInsertBacklinkOption: true,
onSubmit: createModifyNodeModalSubmitHandler(plugin, editor),
}).open();
}
Expand All @@ -103,6 +106,7 @@ export const registerCommands = (plugin: DiscourseGraphPlugin) => {
plugin,
currentFile,
initialTitle: selectedText,
showInsertBacklinkOption: !!editor,
onSubmit: createModifyNodeModalSubmitHandler(plugin, editor),
}).open();
},
Expand Down
Loading