-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
laurent22
merged 4 commits into
laurent22:dev
from
personalizedrefrigerator:pr/plugins/note-id-changes
Feb 17, 2025
Merged
Desktop,Mobile: Plugins: Simplify getting the ID of the note open in an editor #11841
laurent22
merged 4 commits into
laurent22:dev
from
personalizedrefrigerator:pr/plugins/note-id-changes
Feb 17, 2025
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Adds an API that allows watching for note ID changes and getting the ID of the note currently open in the editor.
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'); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests demonstrate:
- Getting the current note ID using
state.facet
. - Using a CodeMirror
transactionExtender
to watch for changes to the note ID open in the editor. If the note body changes, thesetNoteIdEffect
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.
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
Testing
This pull request includes automated tests.
Additionally, it has been verified that:
(Both of the above cases also have integration tests.)