mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-17 08:34:54 -04:00
fix(frontend): make note details in redux optional
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
parent
118f158ad1
commit
6698450461
50 changed files with 278 additions and 135 deletions
4
frontend/src/redux/application-state.d.ts
vendored
4
frontend/src/redux/application-state.d.ts
vendored
|
@ -6,17 +6,17 @@
|
|||
import type { HistoryEntryWithOrigin } from '../api/history/types'
|
||||
import type { DarkModeConfig } from './dark-mode/types'
|
||||
import type { EditorConfig } from './editor/types'
|
||||
import type { NoteDetails } from './note-details/types/note-details'
|
||||
import type { RealtimeStatus } from './realtime/types'
|
||||
import type { RendererStatus } from './renderer-status/types'
|
||||
import type { OptionalUserState } from './user/types'
|
||||
import type { OptionalNoteDetails } from './note-details/types/note-details'
|
||||
|
||||
export interface ApplicationState {
|
||||
user: OptionalUserState
|
||||
history: HistoryEntryWithOrigin[]
|
||||
editorConfig: EditorConfig
|
||||
darkMode: DarkModeConfig
|
||||
noteDetails: NoteDetails
|
||||
noteDetails: OptionalNoteDetails
|
||||
rendererStatus: RendererStatus
|
||||
realtimeStatus: RealtimeStatus
|
||||
}
|
||||
|
|
|
@ -73,9 +73,19 @@ export const updateCursorPositions = (selection: CursorSelection): void => {
|
|||
* Updates the current note's metadata from the server.
|
||||
*/
|
||||
export const updateMetadata = async (): Promise<void> => {
|
||||
const updatedMetadata = await getNoteMetadata(store.getState().noteDetails.id)
|
||||
const noteDetails = store.getState().noteDetails
|
||||
if (!noteDetails) {
|
||||
return
|
||||
}
|
||||
const updatedMetadata = await getNoteMetadata(noteDetails.id)
|
||||
store.dispatch({
|
||||
type: NoteDetailsActionType.UPDATE_METADATA,
|
||||
updatedMetadata
|
||||
} as UpdateMetadataAction)
|
||||
}
|
||||
|
||||
export const unloadNote = (): void => {
|
||||
store.dispatch({
|
||||
type: NoteDetailsActionType.UNLOAD_NOTE
|
||||
})
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { buildStateFromUpdatedMarkdownContent } from './build-state-from-updated-markdown-content'
|
||||
import { initialState } from './initial-state'
|
||||
import { buildStateFromFirstHeadingUpdate } from './reducers/build-state-from-first-heading-update'
|
||||
import { buildStateFromMetadataUpdate } from './reducers/build-state-from-metadata-update'
|
||||
import { buildStateFromServerPermissions } from './reducers/build-state-from-server-permissions'
|
||||
|
@ -12,13 +11,19 @@ import { buildStateFromServerDto } from './reducers/build-state-from-set-note-da
|
|||
import { buildStateFromUpdateCursorPosition } from './reducers/build-state-from-update-cursor-position'
|
||||
import type { NoteDetailsActions } from './types'
|
||||
import { NoteDetailsActionType } from './types'
|
||||
import type { NoteDetails } from './types/note-details'
|
||||
import type { OptionalNoteDetails } from './types/note-details'
|
||||
import type { Reducer } from 'redux'
|
||||
|
||||
export const NoteDetailsReducer: Reducer<NoteDetails, NoteDetailsActions> = (
|
||||
state: NoteDetails = initialState,
|
||||
export const NoteDetailsReducer: Reducer<OptionalNoteDetails, NoteDetailsActions> = (
|
||||
state: OptionalNoteDetails = null,
|
||||
action: NoteDetailsActions
|
||||
) => {
|
||||
if (action.type === NoteDetailsActionType.SET_NOTE_DATA_FROM_SERVER) {
|
||||
return buildStateFromServerDto(action.noteFromServer)
|
||||
}
|
||||
if (state === null) {
|
||||
return null
|
||||
}
|
||||
switch (action.type) {
|
||||
case NoteDetailsActionType.UPDATE_CURSOR_POSITION:
|
||||
return buildStateFromUpdateCursorPosition(state, action.selection)
|
||||
|
@ -28,10 +33,10 @@ export const NoteDetailsReducer: Reducer<NoteDetails, NoteDetailsActions> = (
|
|||
return buildStateFromServerPermissions(state, action.notePermissionsFromServer)
|
||||
case NoteDetailsActionType.UPDATE_NOTE_TITLE_BY_FIRST_HEADING:
|
||||
return buildStateFromFirstHeadingUpdate(state, action.firstHeading)
|
||||
case NoteDetailsActionType.SET_NOTE_DATA_FROM_SERVER:
|
||||
return buildStateFromServerDto(action.noteFromServer)
|
||||
case NoteDetailsActionType.UPDATE_METADATA:
|
||||
return buildStateFromMetadataUpdate(state, action.updatedMetadata)
|
||||
case NoteDetailsActionType.UNLOAD_NOTE:
|
||||
return null
|
||||
default:
|
||||
return state
|
||||
}
|
||||
|
|
|
@ -14,7 +14,8 @@ export enum NoteDetailsActionType {
|
|||
SET_NOTE_PERMISSIONS_FROM_SERVER = 'note-details/data/permissions/set',
|
||||
UPDATE_NOTE_TITLE_BY_FIRST_HEADING = 'note-details/update-note-title-by-first-heading',
|
||||
UPDATE_CURSOR_POSITION = 'note-details/updateCursorPosition',
|
||||
UPDATE_METADATA = 'note-details/update-metadata'
|
||||
UPDATE_METADATA = 'note-details/update-metadata',
|
||||
UNLOAD_NOTE = 'note-details/unload-note'
|
||||
}
|
||||
|
||||
export type NoteDetailsActions =
|
||||
|
@ -24,6 +25,7 @@ export type NoteDetailsActions =
|
|||
| UpdateNoteTitleByFirstHeadingAction
|
||||
| UpdateCursorPositionAction
|
||||
| UpdateMetadataAction
|
||||
| UnloadNoteAction
|
||||
|
||||
/**
|
||||
* Action for updating the document content of the currently loaded note.
|
||||
|
@ -69,3 +71,7 @@ export interface UpdateMetadataAction extends Action<NoteDetailsActionType> {
|
|||
type: NoteDetailsActionType.UPDATE_METADATA
|
||||
updatedMetadata: NoteMetadata
|
||||
}
|
||||
|
||||
export interface UnloadNoteAction extends Action<NoteDetailsActionType> {
|
||||
type: NoteDetailsActionType.UNLOAD_NOTE
|
||||
}
|
||||
|
|
|
@ -26,3 +26,5 @@ export interface NoteDetails extends Omit<NoteMetadata, UnnecessaryNoteAttribute
|
|||
frontmatter: NoteFrontmatter
|
||||
startOfContentLineOffset: number
|
||||
}
|
||||
|
||||
export type OptionalNoteDetails = NoteDetails | null
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue