@@ -35,6 +35,14 @@ import { HeadingNode, QuoteNode } from "@lexical/rich-text"
3535import { CodeNode } from "@lexical/code"
3636import { LinkNode } from "@lexical/link"
3737
38+ const normalizeVisibility = (
39+ v : "PRIVATE" | "PM_ONLY" | "CONTRIBUTORS" | "SHARED" | undefined
40+ ) : "PRIVATE" | "PM_ONLY" | "CONTRIBUTORS" => {
41+ if ( v === "SHARED" ) return "CONTRIBUTORS"
42+ if ( v === "PRIVATE" || v === "PM_ONLY" || v === "CONTRIBUTORS" ) return v
43+ return "PRIVATE"
44+ }
45+
3846function ResetFormatOnEnterPlugin ( ) {
3947 const [ editor ] = useLexicalComposerContext ( )
4048 useEffect ( ( ) => {
@@ -198,16 +206,25 @@ export default function NoteEditor({
198206 const [ lastSavedAt , setLastSavedAt ] = useState < number | null > ( null )
199207 const [ title , setTitle ] = useState < string > ( initialTitle || "" )
200208 const [ visibility , setVisibility ] = useState < "PRIVATE" | "PM_ONLY" | "CONTRIBUTORS" > (
201- initialVisibility
209+ normalizeVisibility ( initialVisibility as any )
202210 )
203211 const didInitialChange = useRef ( false )
204212
213+ useEffect ( ( ) => {
214+ setVisibility ( normalizeVisibility ( initialVisibility as any ) )
215+ } , [ initialVisibility ] )
216+
217+ const effectiveReadOnly = useMemo ( ( ) => {
218+ if ( visibility === "CONTRIBUTORS" && canSetContributors ) return false
219+ return readOnly
220+ } , [ readOnly , visibility , canSetContributors ] )
221+
205222 const initialConfig = useMemo (
206223 ( ) => ( {
207224 namespace : "staple-notes" ,
208225 nodes : [ ListNode , ListItemNode , HeadingNode , QuoteNode , CodeNode , LinkNode ] ,
209226 onError : ( e : any ) => console . error ( e ) ,
210- editable : ! readOnly ,
227+ editable : ! effectiveReadOnly ,
211228 editorState : ( editor : any ) => {
212229 if ( initialJSON ) {
213230 editor . setEditorState ( editor . parseEditorState ( initialJSON ) )
@@ -220,7 +237,7 @@ export default function NoteEditor({
220237 }
221238 } ,
222239 } ) ,
223- [ initialJSON , initialMarkdown , readOnly ]
240+ [ initialJSON , initialMarkdown , effectiveReadOnly ]
224241 )
225242
226243 const save = useCallback (
@@ -234,7 +251,7 @@ export default function NoteEditor({
234251 } )
235252 contentJSON = editorState . toJSON ( )
236253
237- const visibilityForMutation = ( visibility === "CONTRIBUTORS" ? "SHARED" : visibility ) as any
254+ const visibilityForMutation = visibility as any
238255
239256 if ( ! currentNoteId ) {
240257 const created = await createNoteMutation ( {
@@ -287,7 +304,7 @@ export default function NoteEditor({
287304 onSaveAndClose = { ( state ) => save ( state , true ) }
288305 isSaving = { isSaving }
289306 lastSavedAt = { lastSavedAt }
290- readOnly = { readOnly }
307+ readOnly = { effectiveReadOnly }
291308 onClose = { onClose }
292309 visibility = { visibility }
293310 onVisibilityChange = { setVisibility }
@@ -307,7 +324,7 @@ export default function NoteEditor({
307324 < MarkdownShortcutPlugin transformers = { TRANSFORMERS } />
308325 < OnChangePlugin
309326 onChange = { ( state ) => {
310- if ( readOnly ) return
327+ if ( effectiveReadOnly ) return
311328 if ( ! didInitialChange . current ) {
312329 didInitialChange . current = true
313330 return
0 commit comments