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

refactor: update isEditable implementation #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
12 changes: 2 additions & 10 deletions packages/svelte-lexical/src/lib/components/toolbar/Toolbar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

import StateStoreRichTextUpdator from './StateStoreRichTextUpdator.svelte';
import {setContext} from 'svelte';
import {getEditor} from '$lib/core/composerContext.js';
import {onMount} from 'svelte';
import {getEditor, setIsEditable} from '$lib/core/composerContext.js';
import type {NodeKey} from 'lexical';

const editor = getEditor();

const isEditable = writable(editor.isEditable());
setContext('isEditable', isEditable);
setIsEditable(editor)

const activeEditor = writable(editor);
setContext('activeEditor', activeEditor);
Expand All @@ -36,12 +34,6 @@
setContext('codeLanguage', writable(''));
setContext('isLink', writable(false));
setContext('isImageCaption', writable(false));

onMount(() => {
return editor.registerEditableListener((editable) => {
$isEditable = editable;
});
});
</script>

<StateStoreRichTextUpdator />
Expand Down
22 changes: 21 additions & 1 deletion packages/svelte-lexical/src/lib/core/composerContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type {HistoryState} from '@lexical/history';
import type {LexicalEditor} from 'lexical';
import {SvelteComponent, getContext, setContext} from 'svelte';
import type {Writable} from 'svelte/store';
import {writable,type Writable} from 'svelte/store';

export function getEditor(): LexicalEditor {
return getContext('editor');
Expand All @@ -17,6 +17,26 @@ export function getIsEditable(): Writable<boolean> {
return getContext('isEditable');
}

/**
* Writable store that abstracts {@link LexicalEditor.isEditable} and {@link LexicalEditor.setEditable}.
*/
export function setIsEditable(editor: LexicalEditor): Writable<boolean> {
const isEditable = writable(editor.isEditable(), (set) => {
const unsubscribe = editor.registerEditableListener((editable) => {
set(editable)
})
return unsubscribe
});

// The overridden `set` method will __not__ update the store's state.
// Assume that the editor will emit an update to the store's callback at `registerEditableListener.
isEditable.set = (editable) => {
editor.setEditable(editable)
}

return setContext('isEditable', isEditable)
}

export function getActiveEditor(): Writable<LexicalEditor> {
return getContext('activeEditor');
}
Expand Down