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
28 changes: 26 additions & 2 deletions apps/obsidian/src/components/ModifyNodeModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { App, Modal, Notice, TFile } from "obsidian";
import { App, MarkdownView, Modal, Notice, TFile } from "obsidian";
import { createRoot, Root } from "react-dom/client";
import {
StrictMode,
Expand Down Expand Up @@ -49,6 +49,7 @@ 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;
Expand Down Expand Up @@ -83,6 +84,9 @@ export const ModifyNodeForm = ({
const [selectedRelationshipKey, setSelectedRelationshipKey] = useState<
string | undefined
>(undefined);
const hasEditorContext =
!!plugin.app.workspace.getActiveViewOfType(MarkdownView);
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 +282,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 +296,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 +397,7 @@ export const ModifyNodeForm = ({
selectedExistingNode: selectedExistingNode || undefined,
relationshipId: selectedRel?.uniqueKey || undefined,
relationshipTargetFile: currentFile || undefined,
insertBacklink,
});
onCancel();
} catch (error) {
Expand Down Expand Up @@ -418,6 +425,7 @@ export const ModifyNodeForm = ({
selectedRelationshipKey,
currentFile,
availableRelationships,
insertBacklink,
]);

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

{!isEditMode && hasEditorContext && (
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Hide backlink toggle when submit handler can't honor it

Rendering the checkbox whenever hasEditorContext is true exposes it in create flows whose onSubmit handlers do not consume insertBacklink (for example, the image-conversion and tag-node handlers), so users can uncheck Insert backlink and still get link replacement behavior. This makes the new control a no-op in those contexts and breaks the expectation that manual toggles are respected; gate this UI on caller support (or thread insertBacklink through all submit handlers).

Useful? React with 👍 / 👎.

<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,6 +628,7 @@ type ModifyNodeModalProps = {
selectedExistingNode?: TFile;
relationshipId?: string;
relationshipTargetFile?: TFile;
insertBacklink: boolean;
}) => Promise<void>;
initialTitle?: string;
initialNodeType?: DiscourseNode;
Expand All @@ -622,6 +645,7 @@ class ModifyNodeModal extends Modal {
selectedExistingNode?: TFile;
relationshipId?: string;
relationshipTargetFile?: TFile;
insertBacklink: boolean;
}) => Promise<void>;
private root: Root | null = null;
private initialTitle?: string;
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
8 changes: 5 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
Loading