Skip to content
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

Desktop,Mobile: Plugins: Simplify getting the ID of the note open in an editor #11841

Merged

Conversation

personalizedrefrigerator
Copy link
Collaborator

@personalizedrefrigerator personalizedrefrigerator commented Feb 16, 2025

Summary

This pull request simplifies determining the ID for the note currently open in a CodeMirror Markdown editor.

See the comment below for example use.

Use cases

This may be useful in:

  • Extra Markdown Editor Settings, to allow resolving Code folding issue personalizedrefrigerator/joplin-plugin-extra-editor-settings#3. Currently, it's difficult to determine the ID associated with the note in the editor (e.g. immediately before/after the user switches notes). Knowing the note ID would simplify saving/loading which lines were last folded in a note.
  • Diff View plugin: It's currently difficult to determine which note is associated with a diff (particularly for background windows in multi-window mode).
  • This may also allow simplifying the cursor restore logic in @alondmnt's "Resume Note Plugin", depending on the implementation.

Testing

This pull request includes automated tests.

Additionally, it has been verified that:

  • Joplin starts and the note editor loads on desktop.
  • Joplin starts and the note editor loads on mobile/web.

(Both of the above cases also have integration tests.)

Comment on lines +212 to +249
it('updateBody should update the note ID facet and dispatch changes', () => {
const control = createEditorControl('test');
const noteIdFacet = control.joplinExtensions.noteIdFacet;
const getFacet = () => control.editor.state.facet(noteIdFacet);

control.updateBody('Test', { noteId: 'updated' });
expect(getFacet()).toBe('updated');

// Updating the ID without updating the body should change the ID
control.updateBody('Test', { noteId: 'updated 2' });
expect(getFacet()).toBe('updated 2');

// Changing the body, without specifying a new note ID, should
// not update the facet.
control.updateBody('Test 2');
expect(getFacet()).toBe('updated 2');
});

it('updateBody should dispatch changes to the note ID', () => {
const control = createEditorControl('test');

let noteId = '';
const noteIdChangeListener = EditorState.transactionExtender.of(transaction => {
for (const effect of transaction.effects) {
if (effect.is(control.joplinExtensions.setNoteIdEffect)) {
noteId = effect.value;
}
}
return null;
});
control.addExtension(noteIdChangeListener);

control.updateBody('Test', { noteId: 'updated' });
expect(noteId).toBe('updated');

control.updateBody('Test', { noteId: 'updated-2' });
expect(noteId).toBe('updated-2');
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

These tests demonstrate:

  1. Getting the current note ID using state.facet.
  2. Using a CodeMirror transactionExtender to watch for changes to the note ID open in the editor. If the note body changes, the setNoteIdEffect is included in the same transaction. One possible use case is determining whether a change to the note body was caused by a user or switching to a new note.

@personalizedrefrigerator personalizedrefrigerator changed the title Desktop,Mobile: Plugins: Simplify setting per-note editor settings Desktop,Mobile: Plugins: Simplify getting the ID of the note open in an editor Feb 16, 2025
@laurent22
Copy link
Owner

There are some test unit errors but the stack trace doesn't make much sense to me so not sure if they are random or due to this change

@personalizedrefrigerator personalizedrefrigerator added the plugins Anything related to Joplin's plugin system label Feb 16, 2025
@laurent22 laurent22 merged commit 316ef9d into laurent22:dev Feb 17, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugins Anything related to Joplin's plugin system
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants