Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext
import { SNNote, ContentType } from '@standardnotes/snjs'
import { useState, useEffect } from 'react'

const ReadonlyPlugin = ({ note }: { note: SNNote }) => {
const ReadonlyPlugin = ({ note, forceReadonly = false }: { note: SNNote; forceReadonly?: boolean }) => {
const application = useApplication()
const [editor] = useLexicalComposerContext()
const [readOnly, setReadOnly] = useState(note.locked)
const [noteLocked, setNoteLocked] = useState(note.locked)

useEffect(() => {
return application.items.streamItems<SNNote>(ContentType.TYPES.Note, ({ changed }) => {
const changedNoteItem = changed.find((changedItem) => changedItem.uuid === note.uuid)

if (changedNoteItem) {
setReadOnly(changedNoteItem.locked)
setNoteLocked(changedNoteItem.locked)
}
})
}, [application, note.uuid])

useEffect(() => {
editor.update(() => {
editor.setEditable(!readOnly)
editor.setEditable(!(noteLocked || forceReadonly))
})
}, [editor, readOnly])
}, [editor, noteLocked, forceReadonly])

return null
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { WebApplication } from '@/Application/WebApplication'
import {
ApplicationEvent,
isPayloadSourceRetrieved,
NativeFeatureIdentifier,
FeatureStatus,
Expand Down Expand Up @@ -71,7 +72,7 @@ export const SuperEditor: FunctionComponent<Props> = ({
const getMarkdownPlugin = useRef<GetMarkdownPluginInterface | null>(null)
const [featureStatus, setFeatureStatus] = useState<FeatureStatus>(FeatureStatus.Entitled)

useEffect(() => {
const reloadFeatureStatus = useCallback(() => {
setFeatureStatus(
application.features.getFeatureStatus(
NativeFeatureIdentifier.create(NativeFeatureIdentifier.TYPES.SuperEditor).getValue(),
Expand All @@ -82,7 +83,24 @@ export const SuperEditor: FunctionComponent<Props> = ({
)
}, [application.features])

useEffect(() => {
reloadFeatureStatus()
}, [reloadFeatureStatus])

useEffect(() => {
return application.addEventObserver(async (event) => {
switch (event) {
case ApplicationEvent.FeaturesAvailabilityChanged:
case ApplicationEvent.UserRolesChanged:
case ApplicationEvent.LocalDataLoaded:
reloadFeatureStatus()
break
}
})
}, [application, reloadFeatureStatus])

const keyboardService = application.keyboardService
const isEditorReadonly = note.current.locked || Boolean(readonly) || featureStatus !== FeatureStatus.Entitled

useEffect(() => {
return application.commands.addWithShortcut(
Expand Down Expand Up @@ -155,6 +173,9 @@ export const SuperEditor: FunctionComponent<Props> = ({
ignoreNextChange.current = false
return
}
if (isEditorReadonly) {
return
}

void controller.saveAndAwaitLocalPropagation({
text: value,
Expand All @@ -165,7 +186,7 @@ export const SuperEditor: FunctionComponent<Props> = ({
},
})
},
[controller],
[controller, isEditorReadonly],
)

const handleBubbleRemove = useCallback(
Expand Down Expand Up @@ -252,13 +273,13 @@ export const SuperEditor: FunctionComponent<Props> = ({
<ErrorBoundary>
<LinkingControllerProvider controller={linkingController}>
<FilesControllerProvider controller={filesController}>
<BlocksEditorComposer readonly={note.current.locked || readonly} initialValue={note.current.text}>
<BlocksEditorComposer readonly={isEditorReadonly} initialValue={note.current.text}>
<BlocksEditor
onChange={handleChange}
className="blocks-editor h-full resize-none"
previewLength={SuperNotePreviewCharLimit}
spellcheck={spellcheck}
readonly={note.current.locked || readonly}
readonly={isEditorReadonly}
onFocus={handleFocus}
onBlur={onBlur}
>
Expand All @@ -271,7 +292,9 @@ export const SuperEditor: FunctionComponent<Props> = ({
/>
<NodeObserverPlugin nodeType={BubbleNode} onRemove={handleBubbleRemove} />
<NodeObserverPlugin nodeType={FileNode} onRemove={handleBubbleRemove} />
{readonly === undefined && <ReadonlyPlugin note={note.current} />}
{readonly === undefined && (
<ReadonlyPlugin note={note.current} forceReadonly={featureStatus !== FeatureStatus.Entitled} />
)}
<AutoFocusPlugin isEnabled={controller.isTemplateNote} />
<BlockPickerMenuPlugin />
<NoteFromSelectionPlugin currentNote={note.current} />
Expand Down
Loading