-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CARE-1802] Finish transitioning of SnippetsExtension to be self-suff…
…icient UI component Related to 7425e75
- Loading branch information
Showing
5 changed files
with
93 additions
and
82 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
import { useState } from 'react'; | ||
import { useMemo, useState } from 'react'; | ||
import type { BaseEditor, Editor } from 'slate'; | ||
import { ReactEditor } from 'slate-react'; | ||
|
||
import { saveSelection } from '../commands'; | ||
|
||
type SavedSelection = ReturnType<typeof saveSelection>; | ||
|
||
interface Actions { | ||
restore: (editor: ReactEditor & Editor, options?: { focus?: boolean }) => void; | ||
save: (editor: Editor) => void; | ||
} | ||
|
||
export function useSavedSelection(): Actions { | ||
export function useSavedSelection() { | ||
const [savedSelection, setSavedSelection] = useState<SavedSelection | null>(null); | ||
|
||
function restore(editor: ReactEditor & Editor, { focus = false } = {}) { | ||
if (focus) { | ||
ReactEditor.focus(editor); | ||
} | ||
|
||
if (savedSelection) { | ||
savedSelection.restore(editor); | ||
setSavedSelection(null); | ||
} | ||
} | ||
|
||
function save(editor: BaseEditor) { | ||
return setSavedSelection(saveSelection(editor)); | ||
} | ||
|
||
return { restore, save }; | ||
return useMemo( | ||
() => ({ | ||
restore(editor: ReactEditor & Editor, { focus = false } = {}) { | ||
if (focus) { | ||
ReactEditor.focus(editor); | ||
} | ||
|
||
if (savedSelection) { | ||
savedSelection.restore(editor); | ||
setSavedSelection(null); | ||
} | ||
}, | ||
save(editor: BaseEditor) { | ||
return setSavedSelection(saveSelection(editor)); | ||
}, | ||
}), | ||
[savedSelection], | ||
); | ||
} |
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
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
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
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