Changes dispatched to editor in Reading mode don't update the source markdown

Steps to reproduce

  1. Make a note with the text value

In my plugin I am using the following code to update a line in the source markdown:

function updateText(){
	const markdownView =
	   plugin.app.workspace.getActiveViewOfType(MarkdownView);
	  if (markdownView instanceof MarkdownView) {
		const editor = markdownView.editor;
                const value = "new value";
		updateEditorLine(editor, 0, 5, value); 
		await markdownView.save();
	}
}

export const updateEditorText = (
	editor: Editor,
        from: number,
        to: number,
	text: string
) => {
	const cm = (editor as any).cm;
	const state = cm.state.doc;
	cm.dispatch({
		changes: {
                    from,
                    to,
		    insert: text,
		},
		scrollIntoView: false,
	});
};

This code will update the source markdown while in editing mode, but if I got into reading mode, nothing is updated

//Editing mode
//markdown before
value
//markdown after
new value

//Reading mode
//markdown before
value

//markdown after
value

Expected result

Since the markdownView.editor object isn’t null, and I can dispatch calls to the editor and also call editor.save() in reading mode, I expect it to update the source markdown

Actual result

The source markdown is not updated

Environment

  • Operating system:
    Mac OS Big Sur - 11.6.4
  • Debug info:
    SYSTEM INFO:
    Obsidian version: v0.15.9
    Installer version: v0.15.8
    Operating system: Darwin Kernel Version 20.6.0: Wed Jan 12 22:22:45 PST 2022; root:xnu-7195.141.19~2/RELEASE_ARM64_T8101 20.6.0
    Login status: not logged in
    Insider build toggle: off
    Live preview: on
    Legacy editor: off
    Base theme: light
    Community theme: none
    Snippets enabled: 0
    Restricted mode: off
    Plugins installed: 9
    Plugins enabled: 1
    1: Sample Plugin v1.0.0

RECOMMENDATIONS:
Community plugins: for bugs, please first try updating all your plugins to latest. If still not fixed, please try to make the issue happen in the Sandbox Vault or disable community plugins.


Additional information

3 Likes

moved to developers and api.

I want to add to this issue. In my plugin I’m trying to change the source markdown on input checkbox click.

Issue

Changes done in Reading mode don’t update the source markdown. Then, if you toggle editor and undo, the changes appear

Steps to reproduce

  1. Enable a sample plugin with the code below in main.ts
  2. Copy this html tag into a note:
    <input type='checkbox'>
  3. Toggle reading view
  4. Click on the input
  5. Go back to editing view, press Ctrl + Z

Expected result

Source markdown changed and re-rendered in Reading view

Actual result

Changes are removed immediately, but are accessible with Undo

Code

import { MarkdownView, Plugin } from 'obsidian';

export default class MyPlugin extends Plugin {

	async onload() {
		this.registerMarkdownPostProcessor((elem, context) => {
			const inputs = elem.querySelectorAll(`input:not(.task-list-item-checkbox)[type='checkbox']`);

			const handleCheckboxClick = (e: Event) => {
				const view = this.app.workspace.getActiveViewOfType(MarkdownView);
				const editor = view?.editor;
				if (!editor) return;

				const lastLine = editor.getLine(editor.lastLine());
				editor.setLine(editor.lineCount(), lastLine + 'test');
			};

			for (let index = 0; index < inputs.length; index++) {
				const input = inputs.item(index);
				input.addEventListener('click', (e) => handleCheckboxClick(e));
			}
		});
	}

	onunload() {}

}

Debug info

SYSTEM INFO:
	Obsidian version: v1.0.3
	Installer version: v0.15.9
	Operating system: Windows 10 Home Single Language 10.0.19045
	Login status: not logged in
	Insider build toggle: off
	Live preview: on
	Legacy editor: off
	Base theme: dark
	Community theme: none
	Snippets enabled: 1
	Restricted mode: off
	Plugins installed: 5
	Plugins enabled: 1
		1: Sample Plugin v1.0.0

Any updates on this? There seems to be a severe lack of interface between the editor and reading view. Would love to know if there are plans to address this.