import type { AnyEditor as Editor, Selection, Value, } from '../interfaces/editor'; import { SelectionApi } from '../interfaces/selection'; import { readAuthoredViewKey, readShownPreview } from './authored-runtime'; import { isDocumentView } from './document-view-read'; import { getEditorLifetime } from './editor-lifetime'; import { getEditorRuntime, getEditorRuntimeOwner, type InternalEditorRuntime, type ViewFocus, type ViewPolicySource, } from './editor-runtime'; import { notifyObservers, runEditorObserver } from './lifecycle-error'; import { type CaretTarget, getOwnerStore, peekOwnerStore, scheduleMicrotask, } from './owner-store'; import { flushRootTargets, reportShownChange } from './root-target'; import { getSelectionStateCaret } from './selection-state'; import { type EditorViewStateBucket, type NormalizedSelectorDependencies, selectorDependenciesWantShownPreview, selectorDependenciesWantViewKind, } from './selector-dependencies'; import { getViewStateHost } from './state-hosts'; import { getCommittedCarets, samePair, subscribeCaretOwner, } from './view-carets'; import { assertWritablePresentationMode, getBaseViewPresentation, isPresentationComposing as isComposingPresentation, isOwnerComposingState, setOwnerComposing, setPresentationComposing, setPresentationViewing, subscribeOwnerComposingState, } from './view-presentation'; export type ViewStateHost = Readonly<{ getBaseCallRoot: (editor: Editor) => string; }>; const EDITOR_COMPOSITION_PENDING = new WeakMap>(); type EditorViewStateChange = | 'admission' | 'authored' | 'composing' | 'composition' | 'focused' | 'readOnly'; type ViewStateListeners = WeakMap< object, Set<(change: EditorViewStateChange) => void> >; const EDITOR_VIEW_STATE_LISTENERS: ViewStateListeners = new WeakMap(); const VIEW_STATE_FOLLOWS: ViewStateListeners = new WeakMap(); export const STATE_VIEW_OWNERS = new WeakMap(); export const getActiveCaretRoot = (editor: Editor): string | null => { const owner = getEditorRuntimeOwner(editor); const committed = getSelectionStateCaret(owner); return committed.selection ? committed.root : null; }; export const publishActiveCaretRoot = (editor: Editor) => { const owner = getEditorRuntimeOwner(editor); const watch = peekOwnerStore(owner)?.activeCaretRootWatch; if (!watch) return; const nextRoot = getActiveCaretRoot(owner); if (watch.root === nextRoot) return; watch.root = nextRoot; notifyObservers(owner, watch.listeners, 'commit-listener', (listener) => listener() ); }; export const subscribeActiveCaretRoot = ( editor: Editor, listener: () => void ) => { const owner = getEditorRuntimeOwner(editor); const store = getOwnerStore(owner); let watch = store.activeCaretRootWatch; if (!watch) { watch = { listeners: new Set<() => void>(), root: getActiveCaretRoot(owner), stopOwner: subscribeCaretOwner(owner, () => publishActiveCaretRoot(owner) ), }; store.activeCaretRootWatch = watch; } watch.listeners.add(listener); return () => { watch.listeners.delete(listener); if (watch.listeners.size > 0 || store.activeCaretRootWatch !== watch) { return; } watch.stopOwner(); store.activeCaretRootWatch = undefined; }; }; const readCaretTarget = (owner: Editor): CaretTarget => ({ root: getViewStateHost().getBaseCallRoot(owner), view: getCommittedCarets(owner).owner, }); export const publishCaretTarget = (editor: Editor) => { const owner = getEditorRuntimeOwner(editor); const watch = peekOwnerStore(owner)?.caretTargetWatch; if (!watch) return; const previous = watch.target; const target = readCaretTarget(owner); if (previous.root === target.root && samePair(previous.view, target.view)) { return; } watch.target = target; notifyObservers(owner, watch.listeners, 'commit-listener', (listener) => listener() ); }; export const subscribeCaretTarget = (editor: Editor, listener: () => void) => { const owner = getEditorRuntimeOwner(editor); const store = getOwnerStore(owner); let watch = store.caretTargetWatch; if (!watch) { watch = { listeners: new Set<() => void>(), stopOwner: subscribeCaretOwner(owner, () => publishCaretTarget(owner)), target: readCaretTarget(owner), }; store.caretTargetWatch = watch; } watch.listeners.add(listener); return () => { watch.listeners.delete(listener); if (watch.listeners.size > 0 || store.caretTargetWatch !== watch) { return; } watch.stopOwner(); store.caretTargetWatch = undefined; }; }; export const selectionPositionEquals = (left: Selection, right: Selection) => { const withoutPendingMarks = (selection: Selection) => { if (!SelectionApi.isText(selection)) return selection; const { marks: _marks, ...position } = selection; return position; }; return SelectionApi.equals( withoutPendingMarks(left), withoutPendingMarks(right) ); }; const OWNER_READ_ONLY_CONTRIBUTORS = new WeakMap boolean>>(); const hasOwnerReadOnlyContributor = (owner: object): boolean => { const contributors = OWNER_READ_ONLY_CONTRIBUTORS.get(owner); if (!contributors) return false; for (const read of contributors) { if (read()) return true; } return false; }; const readOwnerViewStateFlag = ( editor: Editor, key: 'composing' | 'focused' | 'readOnly' ): boolean => { const store = peekOwnerStore(editor); if (key === 'readOnly') { return ( (store?.modelReadOnly ?? false) || hasOwnerReadOnlyContributor(getEditorRuntimeOwner(editor)) ); } if (!store) return false; if (key === 'composing') return store.baseComposing; return store.baseFocused; }; const writeOwnerViewStateFlag = ( editor: Editor, key: 'composing' | 'focused' | 'readOnly', value: boolean ): boolean => { const store = getOwnerStore(editor); if (key === 'composing') { if (store.baseComposing === value) return false; store.baseComposing = value; return true; } if (key === 'focused') { if (store.baseFocused === value) return false; store.baseFocused = value; return true; } if (store.modelReadOnly === value) return false; store.modelReadOnly = value; return true; }; export const getEditorViewState = ( editor: Editor, key: 'composing' | 'focused' | 'readOnly' ): boolean => { const { getViewState } = getEditorRuntime(editor); if (getViewState) return getViewState(key); return readOwnerViewStateFlag(editor, key); }; const writeEditorViewStateFlag = ( editor: Editor, key: 'composing' | 'focused' | 'readOnly', value: boolean ) => { const { setViewState } = getEditorRuntime(editor); if (setViewState) return setViewState(key, value); return writeOwnerViewStateFlag(editor, key, value); }; const setEditorViewStateFlag = ( editor: Editor, key: 'composing' | 'focused', value: boolean ) => { const changed = writeEditorViewStateFlag(editor, key, value); if (changed) notifyEditorViewState(editor, key); return changed; }; export const isEditorCompositionPending = (editor: Editor) => (EDITOR_COMPOSITION_PENDING.get(getEditorRuntime(editor))?.size ?? 0) > 0; const recordPresentationComposing = (editor: Editor) => { // A view that follows another answers composition from that view, which // records its own. if (getEditorRuntime(editor).policySource?.follows) return; const composing = editor.read.view.isComposing() || isEditorCompositionPending(editor); const runtime = getEditorRuntime(editor); setOwnerComposing(getEditorRuntimeOwner(editor), runtime, composing); if (setPresentationComposing(presentationOf(editor), runtime, composing)) { notifyPresentationViewState(editor, 'composition'); } }; export const isOwnerComposing = (editor: Editor) => isOwnerComposingState(getEditorRuntimeOwner(editor)); export const subscribeOwnerComposition = ( editor: Editor, listener: () => void ) => subscribeOwnerComposingState(getEditorRuntimeOwner(editor), listener); export const isPresentationComposing = (editor: Editor) => isComposingPresentation(presentationOf(editor)); export const subscribePresentationComposition = ( editor: Editor, listener: () => void ) => listenToViewState(presentationOf(editor), (change) => { if (change === 'composition') listener(); }); export const setEditorComposing = (editor: Editor, composing: boolean) => { setEditorViewStateFlag(editor, 'composing', composing); recordPresentationComposing(editor); }; export const setEditorCompositionPending = ( editor: Editor, input: object, pending: boolean ) => { if (isDocumentView(editor)) return; const runtime = getEditorRuntime(editor); const inputs = EDITOR_COMPOSITION_PENDING.get(runtime) ?? new Set(); const was = inputs.size > 0; if (pending) inputs.add(input); else inputs.delete(input); EDITOR_COMPOSITION_PENDING.set(runtime, inputs); if (was !== inputs.size > 0) notifyEditorViewState(editor, 'composition'); recordPresentationComposing(editor); }; export const setEditorFocused = (editor: Editor, focused: boolean) => { setEditorViewStateFlag(editor, 'focused', focused); }; const normalizeEditorMaxLength = (maxLength: number | undefined) => { if (maxLength === undefined) { return undefined; } if (!Number.isSafeInteger(maxLength) || maxLength < 0) { throw new Error('maxLength must be a non-negative safe integer.'); } return maxLength; }; export const getEditorMaxLength = (editor: object): number | undefined => peekOwnerStore(STATE_VIEW_OWNERS.get(editor) ?? (editor as Editor)) ?.maxLength; /** @internal */ export const setEditorMaxLength = ( editor: Editor, maxLength: number | undefined ) => { getOwnerStore(editor).maxLength = normalizeEditorMaxLength(maxLength); }; const reportReadOnlyChange = (editor: Editor, change: () => T): T => { const wasReadOnly = editor.read.view.isReadOnly(); const result = change(); if (editor.read.view.isReadOnly() !== wasReadOnly) { notifyEditorViewState(editor, 'readOnly'); } return result; }; const OWN_READ_ONLY_LISTENERS = new WeakMap< object, (readOnly: boolean) => void >(); export const onOwnViewReadOnlyChange = ( editor: Editor, listener: (readOnly: boolean) => void ) => { OWN_READ_ONLY_LISTENERS.set(getEditorRuntime(editor), listener); }; export const isModelEditorReadOnly = (editor: Editor): boolean => (peekOwnerStore(editor)?.modelReadOnly ?? false) || hasOwnerReadOnlyContributor(getEditorRuntimeOwner(editor)); export const isModelEditorViewing = (editor: Editor): boolean => peekOwnerStore(editor)?.modelReadOnly ?? false; /** @internal */ export const registerOwnerReadOnlyContributor = ( editor: Editor, read: () => boolean ): (() => void) => { const owner = getEditorRuntimeOwner(editor); const contributors = OWNER_READ_ONLY_CONTRIBUTORS.get(owner) ?? new Set(); contributors.add(read); OWNER_READ_ONLY_CONTRIBUTORS.set(owner, contributors); return () => { contributors.delete(read); if (contributors.size === 0) { OWNER_READ_ONLY_CONTRIBUTORS.delete(owner); } }; }; /** @internal */ export const notifyOwnerReadOnlyContributors = (editor: Editor) => { notifyEditorViewState(getEditorRuntimeOwner(editor), 'readOnly'); }; export const setEditorReadOnly = (editor: Editor, readOnly: boolean) => { reportReadOnlyChange(editor, () => { const changed = writeEditorViewStateFlag(editor, 'readOnly', readOnly); if (changed) { OWN_READ_ONLY_LISTENERS.get(getEditorRuntime(editor))?.(readOnly); } return changed; }); }; const presentationOf = (editor: Editor) => getEditorRuntime(editor).presentation ?? getBaseViewPresentation(getEditorRuntimeOwner(editor)); /** * Put the editor root that shows this view in Viewing, or take it out. Every * view of that editor root, nested ones included, turns read-only at once, * while another editor root of the same editor keeps its own Mode. A lock * from `setEditorReadOnly` stays on its view and the views nested in it, and * leaving Viewing keeps it. An editor that no editor root shows, such as the * one `createEditor` returns, has only that lock, so on that editor Viewing * is the lock `setEditorReadOnly` writes and leaving Viewing unlocks it. */ export const setEditorViewing = (editor: Editor, viewing: boolean) => { const runtime = getEditorRuntime(editor); assertWritablePresentationMode(runtime, 'viewing'); const { presentation } = runtime; if (!presentation) { setEditorReadOnly(editor, viewing); return; } if (setPresentationViewing(presentation, viewing)) { notifyPresentationViewState(editor, 'readOnly'); } }; export const registerEditorSurface = (editor: Editor, readOnly: boolean) => { const { registerSurface } = getEditorRuntime(editor); if (!registerSurface) return () => {}; const report = (change: () => T) => reportReadOnlyChange(editor, () => reportShownChange(editor, change)); const release = report(() => registerSurface(readOnly)); return () => report(release); }; export const hasEditorSurface = (editor: Editor): boolean => getEditorRuntime(editor).hasSurface?.() ?? false; type ViewStateNotice = Readonly<{ change: EditorViewStateChange; editor: Editor; }>; const PENDING_VIEW_STATE_NOTICES = new WeakMap>(); const deliverViewStateNotice = (channel: object, notice: ViewStateNotice) => { if (!PENDING_VIEW_STATE_NOTICES.get(channel)?.delete(notice)) return; const lifetime = getEditorLifetime(notice.editor); if (lifetime === 'destroyed' || lifetime === 'released') return; for (const listeners of [EDITOR_VIEW_STATE_LISTENERS, VIEW_STATE_FOLLOWS]) { notifyObservers( notice.editor, listeners.get(channel), 'commit-listener', (listener) => listener(notice.change) ); } }; const notifyViewStateChannel = ( editor: Editor, channel: object, change: EditorViewStateChange ) => { if ( !EDITOR_VIEW_STATE_LISTENERS.get(channel)?.size && !VIEW_STATE_FOLLOWS.get(channel)?.size ) { return; } const notice = { change, editor }; const pending = PENDING_VIEW_STATE_NOTICES.get(channel) ?? new Set(); pending.add(notice); PENDING_VIEW_STATE_NOTICES.set(channel, pending); scheduleMicrotask(() => deliverViewStateNotice(channel, notice)); }; const PENDING_SWITCH_NOTICES = new Set<() => void>(); export const flushEditorViewState = (editor: Editor) => { const channel = getEditorRuntime(editor); flushRootTargets(); const channels = channel.ownerRuntime && channel.ownerRuntime !== channel ? [channel, channel.ownerRuntime] : [channel]; let delivered = false; for (const current of channels) { const notices = [...(PENDING_VIEW_STATE_NOTICES.get(current) ?? [])]; for (const notice of notices) { deliverViewStateNotice(current, notice); delivered = true; } } if (!delivered) return; for (const tell of [...PENDING_SWITCH_NOTICES]) tell(); }; export const notifyEditorViewState = ( editor: Editor, change: EditorViewStateChange ) => notifyViewStateChannel(editor, getEditorRuntime(editor), change); export const notifyPresentationViewState = ( editor: Editor, change: EditorViewStateChange ) => notifyViewStateChannel(editor, presentationOf(editor), change); const policyChannels = ( runtime: Pick< InternalEditorRuntime, 'mountedIn' | 'ownerRuntime' | 'presentation' >, presentation = runtime.presentation ) => { const channels = new Set(presentation ? [presentation] : []); if (runtime.ownerRuntime && runtime.ownerRuntime !== runtime) { channels.add(runtime.ownerRuntime); } for (let next = runtime.mountedIn; next; next = next.mountedIn) { channels.add(next); if (next.presentation) channels.add(next.presentation); } return channels; }; export const getEditorAdmissionRevision = (editor: Editor): number => peekOwnerStore(editor)?.admissionRevision ?? 0; /** @internal */ export const notifyEditorAdmissionChange = (editor: Editor) => { const owner = getEditorRuntimeOwner(editor); getOwnerStore(owner).admissionRevision += 1; notifyEditorViewState(owner, 'admission'); }; const isPolicyChange = (change: EditorViewStateChange) => change === 'admission' || change === 'authored' || change === 'readOnly'; const listenToViewState = ( runtime: object, listener: (change: EditorViewStateChange) => void, registry = EDITOR_VIEW_STATE_LISTENERS ) => { const listeners = registry.get(runtime) ?? new Set(); listeners.add(listener); registry.set(runtime, listeners); return () => { listeners.delete(listener); if (listeners.size === 0) { registry.delete(runtime); } }; }; type PolicyFollower = Readonly<{ hearFocus: (key: keyof ViewFocus, now: boolean) => void; hearPolicy: (change: EditorViewStateChange) => void; hearSwitch: () => void; }>; const POLICY_FOLLOWS = new WeakMap< ViewPolicySource, Readonly<{ followers: Set; stop: () => void }> >(); const startPolicyFollow = ( source: ViewPolicySource, followers: ReadonlySet ) => { const tell = (hear: (follower: PolicyFollower) => void) => { for (const follower of [...followers]) { if (followers.has(follower)) hear(follower); } }; const tellSwitch = () => tell((follower) => follower.hearSwitch()); const follow = (target: InternalEditorRuntime | undefined) => { if (!target) return () => {}; // The source names its next view before its switch notice arrives, so a // notice from the view it stopped naming counts as that switch, which has // the listener read both policies. const hearTarget = (change: EditorViewStateChange) => { if (!isPolicyChange(change)) return; if (source.current() === target) { tell((follower) => follower.hearPolicy(change)); } else { tellSwitch(); } }; const stops = [target, ...policyChannels(target)].map((channel) => listenToViewState(channel, hearTarget, VIEW_STATE_FOLLOWS) ); stops.push( joinPolicyFollow(target, { hearFocus: source.follows ? (change) => { const now = source.focus()?.[change] ?? false; tell((follower) => follower.hearFocus(change, now)); } : () => {}, hearPolicy: hearTarget, hearSwitch: tellSwitch, }) ); if (source.follows) { stops.push( listenToViewState( target, (change) => { if (change !== 'focused' && change !== 'composing') return; const now = source.focus()?.[change] ?? false; tell((follower) => follower.hearFocus(change, now)); }, VIEW_STATE_FOLLOWS ) ); } return () => { for (const stop of stops) stop(); }; }; let followed = source.current(); let stopFollowed = follow(followed); const stopSource = source.follows?.subscribe(() => { const next = source.current(); if (next === followed) return; stopFollowed(); followed = next; stopFollowed = follow(next); tellSwitch(); const focus = source.focus(); tell((follower) => { follower.hearFocus('focused', focus?.focused ?? false); follower.hearFocus('composing', focus?.composing ?? false); }); }); return () => { stopSource?.(); stopFollowed(); }; }; const joinPolicyFollow = ( { policySource: source }: Pick, follower: PolicyFollower ): (() => void) => { if (!source) return () => {}; let shared = POLICY_FOLLOWS.get(source); if (!shared) { const followers = new Set(); shared = { followers, stop: startPolicyFollow(source, followers) }; POLICY_FOLLOWS.set(source, shared); } const { followers, stop } = shared; followers.add(follower); return () => { followers.delete(follower); if (followers.size > 0) return; POLICY_FOLLOWS.delete(source); stop(); }; }; const listenToOwnerAuthoredView = ( editor: Editor, listener: (change: EditorViewStateChange) => void ) => { const base = getBaseViewPresentation(getEditorRuntimeOwner(editor)); if (base === presentationOf(editor)) return () => {}; return listenToViewState(base, (change) => { if (change === 'authored') listener(change); }); }; export const subscribeEditorViewState = ( editor: Editor, listener: (change: EditorViewStateChange) => void ) => { const runtime = getEditorRuntime(editor); let subscribed = true; // A switch sends a focus and a composition notice whether or not the answer // changed, and the followed view sends its own, so only a changed answer // reaches the listener. const heard = { composing: editor.read.view.isComposing(), focused: editor.read.view.isFocused(), }; const hearFocus = (key: keyof ViewFocus, now: boolean) => { if (!subscribed || now === heard[key]) return; heard[key] = now; runEditorObserver(editor, 'commit-listener', () => listener(key)); }; const hearPolicy = (change: EditorViewStateChange) => { // A pending switch notice already has the listener read both policies // of the view it now follows, so that view's own notice would repeat it. if (!subscribed || PENDING_SWITCH_NOTICES.has(tellSwitch)) return; runEditorObserver(editor, 'commit-listener', () => listener(change)); }; const tellSwitch = () => { if (!PENDING_SWITCH_NOTICES.delete(tellSwitch)) return; for (const change of ['readOnly', 'authored'] as const) { if (!subscribed) return; runEditorObserver(editor, 'commit-listener', () => listener(change)); } }; const hearSwitch = () => { if (PENDING_SWITCH_NOTICES.has(tellSwitch)) return; PENDING_SWITCH_NOTICES.add(tellSwitch); scheduleMicrotask(tellSwitch); }; const hearOwnPolicy = (change: EditorViewStateChange) => { if (isPolicyChange(change)) listener(change); }; const stops = [ listenToViewState(runtime, listener), ...[...policyChannels(runtime, presentationOf(editor))].map((channel) => listenToViewState(channel, hearOwnPolicy) ), joinPolicyFollow(runtime, { hearFocus, hearPolicy, hearSwitch }), listenToOwnerAuthoredView(editor, listener), ]; return () => { subscribed = false; for (const stop of stops) stop(); }; }; export type SharedViewStateSubscription = | 'any' | EditorViewStateBucket | readonly EditorViewStateBucket[]; type SharedViewStateHub = Readonly<{ any: Set<() => void>; authored: Set<() => void>; composing: Set<() => void>; focused: Set<() => void>; readOnly: Set<() => void>; stop: () => void; }>; const SHARED_VIEW_STATE_HUBS = new WeakMap(); const bucketForViewStateChange = ( change: Exclude ): EditorViewStateBucket => (change === 'composition' ? 'composing' : change); export const subscribeSharedEditorViewState = ( editor: Editor, kinds: SharedViewStateSubscription, listener: () => void ): (() => void) => { if (Array.isArray(kinds) && kinds.length === 0) return () => {}; const runtime = getEditorRuntime(editor); let hub = SHARED_VIEW_STATE_HUBS.get(runtime); if (!hub) { const any = new Set<() => void>(); const authored = new Set<() => void>(); const composing = new Set<() => void>(); const focused = new Set<() => void>(); const readOnly = new Set<() => void>(); const buckets: Record void>> = { authored, composing, focused, readOnly, }; const notifyBucket = (listeners: Set<() => void>) => { if (listeners.size === 0) return; notifyObservers(editor, listeners, 'commit-listener', (notify) => { notify(); }); }; const stop = subscribeEditorViewState(editor, (change) => { notifyBucket(any); if (change === 'admission') { notifyBucket(readOnly); notifyBucket(authored); return; } notifyBucket(buckets[bucketForViewStateChange(change)]); }); hub = { any, authored, composing, focused, readOnly, stop }; SHARED_VIEW_STATE_HUBS.set(runtime, hub); } const shared = hub; const registration = () => { listener(); }; const targets: Array void>> = kinds === 'any' ? [shared.any] : typeof kinds === 'string' ? [shared[kinds]] : [...new Set(kinds)].map((kind) => shared[kind]); for (const target of targets) target.add(registration); let active = true; return () => { if (!active) return; active = false; for (const target of targets) target.delete(registration); if ( shared.any.size > 0 || shared.authored.size > 0 || shared.composing.size > 0 || shared.focused.size > 0 || shared.readOnly.size > 0 ) { return; } shared.stop(); if (SHARED_VIEW_STATE_HUBS.get(runtime) === shared) { SHARED_VIEW_STATE_HUBS.delete(runtime); } }; }; export type EditorViewStateFacts = Readonly<{ admissionRevision: number; authoredKey: string | undefined; composing: boolean; compositionPending: boolean; focused: boolean; readOnly: boolean; shownPreview: ReturnType; viewing: boolean; }>; const EMPTY_VIEW_STATE_FACTS: EditorViewStateFacts = Object.freeze({ admissionRevision: 0, authoredKey: undefined, composing: false, compositionPending: false, focused: false, readOnly: false, shownPreview: undefined, viewing: false, }); export const readEditorViewStateFacts = ( editor: Editor, deps?: NormalizedSelectorDependencies ): EditorViewStateFacts => { if (!deps) { return { admissionRevision: getEditorAdmissionRevision(editor), authoredKey: readAuthoredViewKey(editor), composing: editor.read.view.isComposing(), compositionPending: isEditorCompositionPending(editor), focused: editor.read.view.isFocused(), readOnly: editor.read.view.isReadOnly(), shownPreview: readShownPreview(editor), viewing: editor.read.view.isViewing(), }; } const wantsAuthored = selectorDependenciesWantViewKind(deps, 'authored'); const wantsComposing = selectorDependenciesWantViewKind(deps, 'composing'); const wantsFocused = selectorDependenciesWantViewKind(deps, 'focused'); const wantsReadOnly = selectorDependenciesWantViewKind(deps, 'readOnly'); const wantsShownPreview = selectorDependenciesWantShownPreview(deps); const wantsAdmission = wantsReadOnly || wantsShownPreview; if ( !wantsAuthored && !wantsComposing && !wantsFocused && !wantsReadOnly && !wantsShownPreview ) { return EMPTY_VIEW_STATE_FACTS; } return { admissionRevision: wantsAdmission ? getEditorAdmissionRevision(editor) : 0, authoredKey: wantsAuthored ? readAuthoredViewKey(editor) : undefined, composing: wantsComposing ? editor.read.view.isComposing() : false, compositionPending: wantsComposing ? isEditorCompositionPending(editor) : false, focused: wantsFocused ? editor.read.view.isFocused() : false, readOnly: wantsReadOnly ? editor.read.view.isReadOnly() : false, shownPreview: wantsShownPreview ? readShownPreview(editor) : undefined, viewing: wantsReadOnly ? editor.read.view.isViewing() : false, }; }; export const viewStateFactsChanged = ( previous: EditorViewStateFacts, next: EditorViewStateFacts, deps: NormalizedSelectorDependencies ): boolean => { const wantsReadOnly = selectorDependenciesWantViewKind(deps, 'readOnly'); const wantsShownPreview = selectorDependenciesWantShownPreview(deps); if ( (wantsReadOnly || wantsShownPreview) && previous.admissionRevision !== next.admissionRevision ) { return true; } if ( wantsReadOnly && (previous.readOnly !== next.readOnly || previous.viewing !== next.viewing) ) { return true; } if ( selectorDependenciesWantViewKind(deps, 'focused') && previous.focused !== next.focused ) { return true; } if ( selectorDependenciesWantViewKind(deps, 'composing') && (previous.composing !== next.composing || previous.compositionPending !== next.compositionPending) ) { return true; } if ( selectorDependenciesWantViewKind(deps, 'authored') && previous.authoredKey !== next.authoredKey ) { return true; } if (wantsShownPreview && previous.shownPreview !== next.shownPreview) { return true; } return false; };