Skip to content

Commit

Permalink
fix: diff/source toggling takes updated values from the nested editors
Browse files Browse the repository at this point in the history
Fixes #166
  • Loading branch information
petyosi committed Jan 5, 2024
1 parent 9573753 commit dc74d04
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
17 changes: 16 additions & 1 deletion src/examples/basics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,22 @@ export function Lists() {
}

export function Table() {
return <MDXEditor markdown={tableMarkdown} plugins={[tablePlugin()]} />
return (
<MDXEditor
markdown={tableMarkdown}
plugins={[
tablePlugin(),
diffSourcePlugin(),
toolbarPlugin({
toolbarContents: () => (
<DiffSourceToggleWrapper>
<UndoRedo />
</DiffSourceToggleWrapper>
)
})
]}
/>
)
}

export function Link() {
Expand Down
36 changes: 22 additions & 14 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,24 +706,32 @@ export type ViewMode = 'rich-text' | 'source' | 'diff'
* @group Diff/Source
*/
export const viewMode$ = Cell<ViewMode>('rich-text', (r) => {
function currentNextViewMode() {
return scan(
(prev, next: ViewMode) => {
return {
current: prev.next,
next
}
},
{ current: 'rich-text' as ViewMode, next: 'rich-text' as ViewMode }
)
}
r.sub(r.pipe(viewMode$, currentNextViewMode(), withLatestFrom(markdownSourceEditorValue$)), ([{ current }, markdownSourceFromEditor]) => {
if (current === 'source' || current === 'diff') {
r.pub(setMarkdown$, markdownSourceFromEditor)
}
})

r.sub(
r.pipe(
viewMode$,
scan(
(prev, next) => {
return {
current: prev.next,
next
}
},
{ current: 'rich-text' as ViewMode, next: 'rich-text' as ViewMode }
),
withLatestFrom(markdownSourceEditorValue$)
currentNextViewMode(),
filter((mode) => mode.current === 'rich-text'),
withLatestFrom(activeEditor$)
),
([{ current }, markdownSourceFromEditor]) => {
if (current === 'source' || current === 'diff') {
r.pub(setMarkdown$, markdownSourceFromEditor)
}
([mode, editor]) => {
editor?.dispatchCommand(NESTED_EDITOR_UPDATED_COMMAND, undefined)
}
)
})
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/table/TableEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import styles from '../../styles/ui.module.css'
import { isPartOftheEditorUI } from '../../utils/isPartOftheEditorUI'
import { uuidv4 } from '../../utils/uuid4'
import {
NESTED_EDITOR_UPDATED_COMMAND,
editorRootElementRef$,
exportVisitors$,
iconComponentFor$,
Expand Down Expand Up @@ -394,6 +395,15 @@ const CellEditor: React.FC<CellProps> = ({ focus, setActiveCell, parentEditor, l
return true
},
COMMAND_PRIORITY_CRITICAL
),

editor.registerCommand(
NESTED_EDITOR_UPDATED_COMMAND,
() => {
saveAndFocus(null)
return true
},
COMMAND_PRIORITY_CRITICAL
)
)
}, [colIndex, editor, rootEditor, rowIndex, saveAndFocus, setActiveCell])
Expand Down

0 comments on commit dc74d04

Please sign in to comment.