Skip to content

Commit

Permalink
feat: on change includes a parameter to indicate the reason for change
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Feb 16, 2025
1 parent 0ab6746 commit 8c55f06
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/MDXEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ export interface MDXEditorProps {
/**
* Triggered when the editor value changes. The callback is not throttled, you can use any throttling mechanism
* if you intend to do auto-saving.
* @param initialMarkdownNormalize - set to true if the change is triggered when the initial markdown is set. This can happen due to variety of reasons - for example, additional whitespace, bullet symbols different than the configured ones, etc.
*/
onChange?: (markdown: string) => void
onChange?: (markdown: string, initialMarkdownNormalize: boolean) => void
/**
* Triggered when the markdown parser encounters an error. The payload includes the invalid source and the error message.
*/
Expand Down
19 changes: 15 additions & 4 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,16 @@ export const initialMarkdown$ = Cell('')
*/
export const markdown$ = Cell('')

export const initialMarkdownNormalize$ = Cell(false)
/** @internal */
const markdownSignal$ = Signal<string>((r) => {
r.link(markdown$, markdownSignal$)
r.link(initialMarkdown$, markdown$)
r.sub(initialMarkdown$, (md) => {
r.pubIn({
[initialMarkdownNormalize$]: true,
[markdown$]: md
})
})
})

const mutableMarkdownSignal$ = Signal<string>((r) => {
Expand Down Expand Up @@ -546,6 +552,7 @@ export const createRootEditorSubscription$ = Appender(rootEditorSubscriptions$,
})

r.pub(markdown$, theNewMarkdownValue.trim())
r.pub(initialMarkdownNormalize$, false)
})
},
(rootEditor) => {
Expand Down Expand Up @@ -857,7 +864,7 @@ export const corePlugin = realmPlugin<{
spellCheck: boolean
placeholder?: React.ReactNode
autoFocus: boolean | { defaultSelection?: 'rootStart' | 'rootEnd'; preventScroll?: boolean | undefined }
onChange: (markdown: string) => void
onChange: (markdown: string, initialMarkdownNormalize: boolean) => void
onBlur?: (e: FocusEvent) => void
onError?: (payload: { error: string; source: string }) => void
toMarkdownOptions: NonNullable<LexicalConvertOptions['toMarkdownOptions']>
Expand Down Expand Up @@ -902,7 +909,9 @@ export const corePlugin = realmPlugin<{
})

r.singletonSub(markdownErrorSignal$, params?.onError)
r.singletonSub(mutableMarkdownSignal$, params?.onChange)
r.singletonSub(mutableMarkdownSignal$, (value) => {
params?.onChange(value, r.getValue(initialMarkdownNormalize$))
})
r.singletonSub(onBlur$, params?.onBlur)

// Use the JSX extension to parse HTML
Expand Down Expand Up @@ -961,7 +970,9 @@ export const corePlugin = realmPlugin<{
[readOnly$]: params?.readOnly
})

realm.singletonSub(mutableMarkdownSignal$, params?.onChange)
realm.singletonSub(mutableMarkdownSignal$, (value) => {
params?.onChange(value, realm.getValue(initialMarkdownNormalize$))
})
realm.singletonSub(onBlur$, params?.onBlur)
realm.singletonSub(markdownErrorSignal$, params?.onError)
}
Expand Down

0 comments on commit 8c55f06

Please sign in to comment.