Skip to content

Commit

Permalink
more type updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wobsoriano committed Dec 19, 2022
1 parent 629b52e commit 8ac6811
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
33 changes: 28 additions & 5 deletions packages/lexical-vue/src/composables/useCanShowPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { onUnmounted, readonly, ref } from 'vue'
import { onMounted, onUnmounted, readonly, ref } from 'vue'
import { $canShowPlaceholderCurry } from '@lexical/text'
import type { LexicalEditor } from 'lexical'
import { mergeRegister } from '@lexical/utils'

function canShowPlaceholderFromCurrentEditorState(
editor: LexicalEditor,
): boolean {
const currentCanShowPlaceholder = editor
.getEditorState()
.read($canShowPlaceholderCurry(editor.isComposing()))

return currentCanShowPlaceholder
}

export function useCanShowPlaceholder(editor: LexicalEditor) {
const initialState = editor
Expand All @@ -9,10 +20,22 @@ export function useCanShowPlaceholder(editor: LexicalEditor) {

const canShowPlaceholder = ref(initialState)

const unregisterListener = editor.registerUpdateListener(({ editorState }) => {
const isComposing = editor.isComposing()
canShowPlaceholder.value = editorState.read(
$canShowPlaceholderCurry(isComposing),
function resetCanShowPlaceholder() {
const currentCanShowPlaceholder
= canShowPlaceholderFromCurrentEditorState(editor)
canShowPlaceholder.value = currentCanShowPlaceholder
}

let unregisterListener: () => void

onMounted(() => {
unregisterListener = mergeRegister(
editor.registerUpdateListener(() => {
resetCanShowPlaceholder()
}),
editor.registerEditableListener(() => {
resetCanShowPlaceholder()
}),
)
})

Expand Down
18 changes: 9 additions & 9 deletions packages/lexical-vue/src/composables/useHistory.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { HistoryState } from '@lexical/history'
import type { LexicalEditor } from 'lexical'
import type { Ref } from 'vue'
import { computed, watchEffect } from 'vue'
import { computed, unref, watchPostEffect } from 'vue'

import { createEmptyHistoryState, registerHistory } from '@lexical/history'
import { getRealValue } from '../utils'
import type { MaybeRef } from '../utils'

export function useHistory(
editor: LexicalEditor,
externalHistoryState?: Ref<HistoryState> | HistoryState,
delay?: Ref<number> | number,
editor: MaybeRef<LexicalEditor>,
externalHistoryState?: MaybeRef<HistoryState>,
delay?: MaybeRef<number>,
) {
const historyState = computed<HistoryState>(
() => getRealValue(externalHistoryState) || createEmptyHistoryState(),
() => unref(externalHistoryState) || createEmptyHistoryState(),
)

watchEffect((onInvalidate) => {
const unregisterListener = registerHistory(editor, historyState.value, getRealValue(delay) || 1000)
watchPostEffect((onInvalidate) => {
const unregisterListener = registerHistory(unref(editor), historyState.value, unref(delay) || 1000)

onInvalidate(() => {
unregisterListener()
})
Expand Down

0 comments on commit 8ac6811

Please sign in to comment.