Skip to content

Commit

Permalink
Merge branch 'GentikSolm/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Sep 16, 2023
2 parents 67c204f + 3ad6d7d commit 28bb940
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/MDXEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface MDXEditorProps {
/**
* pass if you would like to have the editor automatically focused when mounted.
*/
autoFocus?: boolean | 'rootStart' | 'rootEnd'
autoFocus?: boolean | { defaultSelection?: 'rootStart' | 'rootEnd'; preventScroll?: boolean }
/**
* Triggered when focus leaves the editor
*/
Expand Down Expand Up @@ -147,7 +147,7 @@ export interface MDXEditorMethods {
/**
* Sets focus on input
*/
focus: (callbackFn?: (() => void) | undefined, defaultSelection?: 'rootStart' | 'rootEnd') => void
focus: (callbackFn?: (() => void) | undefined, opts?: { defaultSelection?: 'rootStart' | 'rootEnd'; preventScroll?: boolean }) => void
}

const RenderRecurisveWrappers: React.FC<{ wrappers: React.ComponentType<{ children: React.ReactNode }>[]; children: React.ReactNode }> = ({
Expand Down Expand Up @@ -208,8 +208,8 @@ const Methods: React.FC<{ mdxRef: React.ForwardedRef<MDXEditorMethods> }> = ({ m
setMarkdown: (markdown) => {
realm.pubKey('setMarkdown', markdown)
},
focus: (callbackFn?: (() => void) | undefined, defaultSelection?: 'rootStart' | 'rootEnd') => {
realm.getKeyValue('rootEditor')?.focus(callbackFn, { defaultSelection })
focus: (callbackFn?: (() => void) | undefined, opts?: { defaultSelection?: 'rootStart' | 'rootEnd'; preventScroll?: boolean }) => {
realm.getKeyValue('rootEditor')?.focus(callbackFn, opts)
}
}
},
Expand Down
16 changes: 13 additions & 3 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const coreSystem = system((r) => {
const contentEditableClassName = r.node<string>('')
const readOnly = r.node<boolean>(false)
const placeholder = r.node<React.ReactNode>('')
const autoFocus = r.node<boolean | 'rootStart' | 'rootEnd'>(false)
const autoFocus = r.node<boolean | { defaultSelection?: 'rootStart' | 'rootEnd'; preventScroll: boolean }>(false)
const inFocus = r.node(false, true)
const currentFormat = r.node(0, true)

Expand Down Expand Up @@ -251,7 +251,17 @@ export const coreSystem = system((r) => {

const autoFocusValue = r.getValue(autoFocus)
if (autoFocusValue) {
setTimeout(() => theRootEditor.focus(noop, { defaultSelection: autoFocusValue === true ? 'rootStart' : autoFocusValue }))
if (autoFocusValue === true) {
// Default 'on' state
setTimeout(() => theRootEditor.focus(noop, { defaultSelection: 'rootStart' }))
return
}
setTimeout(() =>
theRootEditor.focus(noop, {
defaultSelection: autoFocusValue.defaultSelection ?? 'rootStart',
preventScroll: autoFocusValue.preventScroll ?? false
})
)
}
})

Expand Down Expand Up @@ -493,7 +503,7 @@ interface CorePluginParams {
initialMarkdown: string
contentEditableClassName: string
placeholder?: React.ReactNode
autoFocus: boolean | 'rootStart' | 'rootEnd'
autoFocus: boolean | { defaultSelection?: 'rootStart' | 'rootEnd'; preventScroll?: boolean }
onChange: (markdown: string) => void
onBlur?: (e: FocusEvent) => void
toMarkdownOptions: NonNullable<LexicalConvertOptions['toMarkdownOptions']>
Expand Down

0 comments on commit 28bb940

Please sign in to comment.