2020-05-02 17:41:07 +02:00
import * as React from 'react' ;
2023-08-14 19:33:48 +02:00
import { useState , useEffect , useCallback , useRef , useMemo } from 'react' ;
2020-05-02 17:41:07 +02:00
import TinyMCE from './NoteBody/TinyMCE/TinyMCE' ;
import { connect } from 'react-redux' ;
import MultiNoteActions from '../MultiNoteActions' ;
2024-05-21 02:28:19 +02:00
import { htmlToMarkdown } from './utils' ;
2020-05-02 17:41:07 +02:00
import useSearchMarkers from './utils/useSearchMarkers' ;
import useNoteSearchBar from './utils/useNoteSearchBar' ;
import useMessageHandler from './utils/useMessageHandler' ;
import useWindowCommandHandler from './utils/useWindowCommandHandler' ;
import useDropHandler from './utils/useDropHandler' ;
import useMarkupToHtml from './utils/useMarkupToHtml' ;
2024-05-21 02:28:19 +02:00
import useFormNote , { OnLoadEvent , SetFormNote } from './utils/useFormNote' ;
2022-11-14 19:25:41 +02:00
import useEffectiveNoteId from './utils/useEffectiveNoteId' ;
2020-09-15 15:01:07 +02:00
import useFolder from './utils/useFolder' ;
2020-05-02 17:41:07 +02:00
import styles_ from './styles' ;
2024-05-21 02:28:19 +02:00
import { NoteEditorProps , FormNote , OnChangeEvent , NoteBodyEditorProps , AllAssetsOptions , NoteBodyEditorRef } from './utils/types' ;
2020-11-07 17:59:37 +02:00
import CommandService from '@joplin/lib/services/CommandService' ;
2020-09-15 15:01:07 +02:00
import ToolbarButton from '../ToolbarButton/ToolbarButton' ;
import Button , { ButtonLevel } from '../Button/Button' ;
2023-12-13 21:24:58 +02:00
import eventManager , { EventName } from '@joplin/lib/eventManager' ;
2021-09-04 19:11:29 +02:00
import { AppState } from '../../app.reducer' ;
2020-11-07 17:59:37 +02:00
import ToolbarButtonUtils from '@joplin/lib/services/commands/ToolbarButtonUtils' ;
2023-12-13 21:24:58 +02:00
import { _ , _n } from '@joplin/lib/locale' ;
2020-10-20 00:24:40 +02:00
import TagList from '../TagList' ;
2020-11-05 18:58:23 +02:00
import NoteTitleBar from './NoteTitle/NoteTitleBar' ;
2021-01-27 19:42:58 +02:00
import markupLanguageUtils from '../../utils/markupLanguageUtils' ;
2020-11-07 17:59:37 +02:00
import Setting from '@joplin/lib/models/Setting' ;
2020-11-13 19:09:28 +02:00
import stateToWhenClauseContext from '../../services/commands/stateToWhenClauseContext' ;
2020-11-16 13:03:44 +02:00
import ExternalEditWatcher from '@joplin/lib/services/ExternalEditWatcher' ;
2023-07-16 18:42:42 +02:00
import { itemIsReadOnly } from '@joplin/lib/models/utils/readOnly' ;
2020-11-07 17:59:37 +02:00
const { themeStyle } = require ( '@joplin/lib/theme' ) ;
const { substrWithEllipsis } = require ( '@joplin/lib/string-utils' ) ;
2023-01-19 19:19:06 +02:00
import NoteSearchBar from '../NoteSearchBar' ;
2021-01-29 20:45:11 +02:00
import { reg } from '@joplin/lib/registry' ;
2021-01-22 19:41:11 +02:00
import Note from '@joplin/lib/models/Note' ;
import Folder from '@joplin/lib/models/Folder' ;
2024-05-21 02:28:19 +02:00
import bridge from '../../services/bridge' ;
2023-01-19 19:19:06 +02:00
import NoteRevisionViewer from '../NoteRevisionViewer' ;
2023-08-14 19:33:48 +02:00
import { parseShareCache } from '@joplin/lib/services/share/reducer' ;
2023-07-16 18:42:42 +02:00
import useAsyncEffect from '@joplin/lib/hooks/useAsyncEffect' ;
import { ModelType } from '@joplin/lib/BaseModel' ;
import BaseItem from '@joplin/lib/models/BaseItem' ;
import { ErrorCode } from '@joplin/lib/errors' ;
import ItemChange from '@joplin/lib/models/ItemChange' ;
2023-10-22 13:00:19 +02:00
import PlainEditor from './NoteBody/PlainEditor/PlainEditor' ;
2023-09-21 10:12:40 +02:00
import CodeMirror6 from './NoteBody/CodeMirror/v6/CodeMirror' ;
import CodeMirror5 from './NoteBody/CodeMirror/v5/CodeMirror' ;
2023-12-13 21:24:58 +02:00
import { openItemById } from './utils/contextMenu' ;
2024-03-25 14:50:33 +02:00
import getPluginSettingValue from '@joplin/lib/services/plugins/utils/getPluginSettingValue' ;
2023-12-29 18:08:09 +02:00
import { MarkupLanguage } from '@joplin/renderer' ;
2024-05-21 02:28:19 +02:00
import useScrollWhenReadyOptions from './utils/useScrollWhenReadyOptions' ;
import useScheduleSaveCallbacks from './utils/useScheduleSaveCallbacks' ;
2020-05-02 17:41:07 +02:00
2020-07-03 23:32:39 +02:00
const commands = [
require ( './commands/showRevisions' ) ,
] ;
2020-10-09 19:35:46 +02:00
const toolbarButtonUtils = new ToolbarButtonUtils ( CommandService . instance ( ) ) ;
2020-05-04 19:37:22 +02:00
function NoteEditor ( props : NoteEditorProps ) {
2020-05-02 17:41:07 +02:00
const [ showRevisions , setShowRevisions ] = useState ( false ) ;
const [ titleHasBeenManuallyChanged , setTitleHasBeenManuallyChanged ] = useState ( false ) ;
2023-07-16 18:42:42 +02:00
const [ isReadOnly , setIsReadOnly ] = useState < boolean > ( false ) ;
2020-05-02 17:41:07 +02:00
2023-10-22 13:00:19 +02:00
const editorRef = useRef < NoteBodyEditorRef > ( ) ;
2024-05-21 02:28:19 +02:00
const titleInputRef = useRef < HTMLInputElement > ( ) ;
2020-05-02 17:41:07 +02:00
const isMountedRef = useRef ( true ) ;
const noteSearchBarRef = useRef ( null ) ;
2024-05-21 02:28:19 +02:00
const setFormNoteRef = useRef < SetFormNote > ( ) ;
const { saveNoteIfWillChange , scheduleSaveNote } = useScheduleSaveCallbacks ( {
setFormNote : setFormNoteRef , dispatch : props.dispatch , editorRef ,
} ) ;
2020-11-12 21:13:28 +02:00
const formNote_beforeLoad = useCallback ( async ( event : OnLoadEvent ) = > {
2020-05-02 17:41:07 +02:00
await saveNoteIfWillChange ( event . formNote ) ;
setShowRevisions ( false ) ;
2024-05-21 02:28:19 +02:00
} , [ saveNoteIfWillChange ] ) ;
2020-05-02 17:41:07 +02:00
const formNote_afterLoad = useCallback ( async ( ) = > {
setTitleHasBeenManuallyChanged ( false ) ;
} , [ ] ) ;
2022-11-14 19:25:41 +02:00
const effectiveNoteId = useEffectiveNoteId ( props ) ;
2020-05-02 17:41:07 +02:00
const { formNote , setFormNote , isNewNote , resourceInfos } = useFormNote ( {
syncStarted : props.syncStarted ,
2023-08-18 10:31:45 +02:00
decryptionStarted : props.decryptionStarted ,
2022-11-14 19:25:41 +02:00
noteId : effectiveNoteId ,
2020-05-02 17:41:07 +02:00
isProvisional : props.isProvisional ,
titleInputRef : titleInputRef ,
editorRef : editorRef ,
onBeforeLoad : formNote_beforeLoad ,
onAfterLoad : formNote_afterLoad ,
} ) ;
2024-05-21 02:28:19 +02:00
setFormNoteRef . current = setFormNote ;
2020-05-02 17:41:07 +02:00
const formNoteRef = useRef < FormNote > ( ) ;
formNoteRef . current = { . . . formNote } ;
2020-09-15 15:01:07 +02:00
const formNoteFolder = useFolder ( { folderId : formNote.parent_id } ) ;
2020-05-02 17:41:07 +02:00
const {
localSearch ,
onChange : localSearch_change ,
onNext : localSearch_next ,
onPrevious : localSearch_previous ,
onClose : localSearch_close ,
setResultCount : setLocalSearchResultCount ,
showLocalSearch ,
setShowLocalSearch ,
searchMarkers : localSearchMarkerOptions ,
2022-12-31 01:54:04 +02:00
} = useNoteSearchBar ( { noteSearchBarRef } ) ;
2020-05-02 17:41:07 +02:00
// If the note has been modified in another editor, wait for it to be saved
// before loading it in this editor.
// const waitingToSaveNote = props.noteId && formNote.id !== props.noteId && props.editorNoteStatuses[props.noteId] === 'saving';
const styles = styles_ ( props ) ;
2023-12-29 18:08:09 +02:00
const whiteBackgroundNoteRendering = formNote . markup_language === MarkupLanguage . Html ;
2020-10-21 01:23:55 +02:00
const markupToHtml = useMarkupToHtml ( {
themeId : props.themeId ,
2023-12-29 18:08:09 +02:00
whiteBackgroundNoteRendering ,
2020-10-21 01:23:55 +02:00
customCss : props.customCss ,
plugins : props.plugins ,
2024-03-25 14:50:33 +02:00
settingValue : getPluginSettingValue ,
2020-10-21 01:23:55 +02:00
} ) ;
2020-05-02 17:41:07 +02:00
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2021-09-19 13:03:16 +02:00
const allAssets = useCallback ( async ( markupLanguage : number , options : AllAssetsOptions = null ) : Promise < any [ ] > = > {
2021-09-19 19:54:14 +02:00
options = {
contentMaxWidthTarget : '' ,
. . . options ,
} ;
2023-12-29 18:08:09 +02:00
const theme = themeStyle ( options . themeId ? options.themeId : props.themeId ) ;
2020-05-02 17:41:07 +02:00
2020-12-19 19:42:18 +02:00
const markupToHtml = markupLanguageUtils . newMarkupToHtml ( { } , {
2020-05-02 17:41:07 +02:00
resourceBaseUrl : ` file:// ${ Setting . value ( 'resourceDir' ) } / ` ,
2021-05-19 15:00:16 +02:00
customCss : props.customCss ,
2020-05-02 17:41:07 +02:00
} ) ;
2021-09-19 13:03:16 +02:00
return markupToHtml . allAssets ( markupLanguage , theme , {
contentMaxWidth : props.contentMaxWidth ,
contentMaxWidthTarget : options.contentMaxWidthTarget ,
2023-12-29 18:08:09 +02:00
whiteBackgroundNoteRendering : options.whiteBackgroundNoteRendering ,
2021-09-19 13:03:16 +02:00
} ) ;
2021-08-14 13:19:53 +02:00
} , [ props . themeId , props . customCss , props . contentMaxWidth ] ) ;
2020-05-02 17:41:07 +02:00
const handleProvisionalFlag = useCallback ( ( ) = > {
if ( props . isProvisional ) {
props . dispatch ( {
type : 'NOTE_PROVISIONAL_FLAG_CLEAR' ,
id : formNote.id ,
} ) ;
}
2024-05-21 02:28:19 +02:00
} , [ props . isProvisional , formNote . id , props . dispatch ] ) ;
2020-05-02 17:41:07 +02:00
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-05-02 17:41:07 +02:00
const onFieldChange = useCallback ( ( field : string , value : any , changeId = 0 ) = > {
if ( ! isMountedRef . current ) {
// When the component is unmounted, various actions can happen which can
// trigger onChange events, for example the textarea might be cleared.
// We need to ignore these events, otherwise the note is going to be saved
// with an invalid body.
reg . logger ( ) . debug ( 'Skipping change event because the component is unmounted' ) ;
return ;
}
handleProvisionalFlag ( ) ;
const change = field === 'body' ? {
body : value ,
} : {
title : value ,
} ;
const newNote = {
. . . formNote ,
. . . change ,
bodyWillChangeId : 0 ,
bodyChangeId : 0 ,
hasChanged : true ,
} ;
if ( field === 'title' ) {
setTitleHasBeenManuallyChanged ( true ) ;
}
if ( isNewNote && ! titleHasBeenManuallyChanged && field === 'body' ) {
// TODO: Handle HTML/Markdown format
newNote . title = Note . defaultTitle ( value ) ;
}
if ( changeId !== null && field === 'body' && formNote . bodyWillChangeId !== changeId ) {
// Note was changed, but another note was loaded before save - skipping
// The previously loaded note, that was modified, will be saved via saveNoteIfWillChange()
} else {
setFormNote ( newNote ) ;
2024-05-21 02:28:19 +02:00
void scheduleSaveNote ( newNote ) ;
2020-05-02 17:41:07 +02:00
}
2024-05-21 02:28:19 +02:00
} , [ handleProvisionalFlag , formNote , setFormNote , isNewNote , titleHasBeenManuallyChanged , scheduleSaveNote ] ) ;
2020-05-02 17:41:07 +02:00
2020-11-14 02:02:17 +02:00
useWindowCommandHandler ( {
dispatch : props.dispatch ,
setShowLocalSearch ,
noteSearchBarRef ,
editorRef ,
titleInputRef ,
setFormNote ,
} ) ;
2020-05-02 17:41:07 +02:00
const onDrop = useDropHandler ( { editorRef } ) ;
const onBodyChange = useCallback ( ( event : OnChangeEvent ) = > onFieldChange ( 'body' , event . content , event . changeId ) , [ onFieldChange ] ) ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-05-02 17:41:07 +02:00
const onTitleChange = useCallback ( ( event : any ) = > onFieldChange ( 'title' , event . target . value ) , [ onFieldChange ] ) ;
2020-11-05 18:58:23 +02:00
// const onTitleKeydown = useCallback((event:any) => {
// const keyCode = event.keyCode;
2020-05-02 17:41:07 +02:00
2020-11-05 18:58:23 +02:00
// if (keyCode === 9) {
// // TAB
// event.preventDefault();
2020-05-02 17:41:07 +02:00
2020-11-05 18:58:23 +02:00
// if (event.shiftKey) {
// CommandService.instance().execute('focusElement', 'noteList');
// } else {
// CommandService.instance().execute('focusElement', 'noteBody');
// }
// }
// }, [props.dispatch]);
2020-05-02 17:41:07 +02:00
2023-08-14 19:33:48 +02:00
const shareCache = useMemo ( ( ) = > {
return parseShareCache ( props . shareCacheSetting ) ;
} , [ props . shareCacheSetting ] ) ;
2023-07-16 18:42:42 +02:00
useAsyncEffect ( async event = > {
if ( ! formNote . id ) return ;
try {
2023-08-14 19:33:48 +02:00
const result = await itemIsReadOnly ( BaseItem , ModelType . Note , ItemChange . SOURCE_UNSPECIFIED , formNote . id , props . syncUserId , shareCache ) ;
2023-07-16 18:42:42 +02:00
if ( event . cancelled ) return ;
setIsReadOnly ( result ) ;
} catch ( error ) {
if ( error . code === ErrorCode . NotFound ) {
// Can happen if the note has been deleted but a render is
// triggered anyway. It can be ignored.
} else {
throw error ;
}
}
2023-08-14 19:33:48 +02:00
} , [ formNote . id , props . syncUserId , shareCache ] ) ;
2023-07-16 18:42:42 +02:00
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-05-02 17:41:07 +02:00
const onBodyWillChange = useCallback ( ( event : any ) = > {
handleProvisionalFlag ( ) ;
2020-05-21 10:14:33 +02:00
setFormNote ( prev = > {
2020-05-02 17:41:07 +02:00
return {
. . . prev ,
bodyWillChangeId : event.changeId ,
hasChanged : true ,
} ;
} ) ;
props . dispatch ( {
type : 'EDITOR_NOTE_STATUS_SET' ,
id : formNote.id ,
status : 'saving' ,
} ) ;
2024-05-21 02:28:19 +02:00
} , [ formNote , setFormNote , handleProvisionalFlag , props . dispatch ] ) ;
2020-05-02 17:41:07 +02:00
2024-05-21 02:28:19 +02:00
const { scrollWhenReady , clearScrollWhenReady } = useScrollWhenReadyOptions ( {
noteId : formNote.id ,
selectedNoteHash : props.selectedNoteHash ,
lastEditorScrollPercents : props.lastEditorScrollPercents ,
editorRef ,
} ) ;
const onMessage = useMessageHandler ( scrollWhenReady , clearScrollWhenReady , editorRef , setLocalSearchResultCount , props . dispatch , formNote , htmlToMarkdown , markupToHtml ) ;
2020-05-02 17:41:07 +02:00
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-08-02 12:24:54 +02:00
const externalEditWatcher_noteChange = useCallback ( ( event : any ) = > {
2020-05-02 17:41:07 +02:00
if ( event . id === formNote . id ) {
const newFormNote = {
. . . formNote ,
title : event.note.title ,
body : event.note.body ,
} ;
setFormNote ( newFormNote ) ;
}
2024-05-21 02:28:19 +02:00
} , [ formNote , setFormNote ] ) ;
2020-05-02 17:41:07 +02:00
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2023-08-02 12:24:54 +02:00
const onNotePropertyChange = useCallback ( ( event : any ) = > {
2020-05-21 10:14:33 +02:00
setFormNote ( formNote = > {
2020-05-02 17:41:07 +02:00
if ( formNote . id !== event . note . id ) return formNote ;
const newFormNote : FormNote = { . . . formNote } ;
for ( const key in event . note ) {
if ( key === 'id' ) continue ;
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
2020-05-02 17:41:07 +02:00
( newFormNote as any ) [ key ] = event . note [ key ] ;
}
return newFormNote ;
} ) ;
2024-05-21 02:28:19 +02:00
} , [ setFormNote ] ) ;
2020-05-02 17:41:07 +02:00
useEffect ( ( ) = > {
2023-12-13 21:24:58 +02:00
eventManager . on ( EventName . AlarmChange , onNotePropertyChange ) ;
2020-05-02 17:41:07 +02:00
ExternalEditWatcher . instance ( ) . on ( 'noteChange' , externalEditWatcher_noteChange ) ;
return ( ) = > {
2023-12-13 21:24:58 +02:00
eventManager . off ( EventName . AlarmChange , onNotePropertyChange ) ;
2020-05-02 17:41:07 +02:00
ExternalEditWatcher . instance ( ) . off ( 'noteChange' , externalEditWatcher_noteChange ) ;
} ;
} , [ externalEditWatcher_noteChange , onNotePropertyChange ] ) ;
2020-07-03 23:32:39 +02:00
useEffect ( ( ) = > {
const dependencies = {
setShowRevisions ,
2020-05-02 17:41:07 +02:00
} ;
2020-07-03 23:32:39 +02:00
CommandService . instance ( ) . componentRegisterCommands ( dependencies , commands ) ;
2020-05-02 17:41:07 +02:00
2020-07-03 23:32:39 +02:00
return ( ) = > {
CommandService . instance ( ) . componentUnregisterCommands ( commands ) ;
} ;
} , [ setShowRevisions ] ) ;
2020-05-02 17:41:07 +02:00
2023-09-21 10:12:40 +02:00
const onScroll = useCallback ( ( event : { percent : number } ) = > {
2020-05-02 17:41:07 +02:00
props . dispatch ( {
type : 'EDITOR_SCROLL_PERCENT_SET' ,
2021-12-15 20:03:20 +02:00
// In callbacks of setTimeout()/setInterval(), props/state cannot be used
// to refer the current value, since they would be one or more generations old.
// For the purpose, useRef value should be used.
noteId : formNoteRef.current.id ,
2020-05-02 17:41:07 +02:00
percent : event.percent ,
} ) ;
2023-10-24 11:47:19 +02:00
} , [ props . dispatch ] ) ;
2020-05-02 17:41:07 +02:00
2024-05-21 02:28:19 +02:00
function renderNoNotes ( rootStyle : React.CSSProperties ) {
2023-06-01 13:02:36 +02:00
const emptyDivStyle = {
backgroundColor : 'black' ,
opacity : 0.1 ,
. . . rootStyle ,
} ;
2020-05-02 17:41:07 +02:00
return < div style = { emptyDivStyle } > < / div > ;
}
2020-09-15 15:01:07 +02:00
function renderTagButton() {
return < ToolbarButton
themeId = { props . themeId }
2020-10-09 19:35:46 +02:00
toolbarButtonInfo = { props . setTagsToolbarButtonInfo }
2020-09-15 15:01:07 +02:00
/ > ;
}
2020-05-17 15:01:42 +02:00
function renderTagBar() {
2020-09-15 15:01:07 +02:00
const theme = themeStyle ( props . themeId ) ;
2020-09-29 12:33:22 +02:00
const noteIds = [ formNote . id ] ;
2021-01-25 12:20:56 +02:00
const instructions = < span onClick = { ( ) = > { void CommandService . instance ( ) . execute ( 'setTags' , noteIds ) ; } } style = { { . . . theme . clickableTextStyle , whiteSpace : 'nowrap' } } > { _ ( 'Click to add tags...' ) } < / span > ;
2020-09-29 12:33:22 +02:00
const tagList = props . selectedNoteTags . length ? < TagList items = { props . selectedNoteTags } / > : null ;
2020-09-15 15:01:07 +02:00
return (
2020-09-29 12:33:22 +02:00
< div style = { { paddingLeft : 8 , display : 'flex' , flexDirection : 'row' , alignItems : 'center' } } > { tagList } { instructions } < / div >
2020-09-15 15:01:07 +02:00
) ;
2020-05-17 15:01:42 +02:00
}
2020-09-06 14:07:00 +02:00
const searchMarkers = useSearchMarkers ( showLocalSearch , localSearchMarkerOptions , props . searches , props . selectedSearchId , props . highlightedWords ) ;
2020-05-02 17:41:07 +02:00
2020-11-12 21:13:28 +02:00
const editorProps : NoteBodyEditorProps = {
2020-05-02 17:41:07 +02:00
ref : editorRef ,
contentKey : formNote.id ,
style : styles.tinyMCE ,
2023-12-29 18:08:09 +02:00
whiteBackgroundNoteRendering ,
2020-05-02 17:41:07 +02:00
onChange : onBodyChange ,
onWillChange : onBodyWillChange ,
onMessage : onMessage ,
content : formNote.body ,
contentMarkupLanguage : formNote.markup_language ,
contentOriginalCss : formNote.originalCss ,
resourceInfos : resourceInfos ,
2023-12-06 21:17:16 +02:00
resourceDirectory : Setting.value ( 'resourceDir' ) ,
2020-05-02 17:41:07 +02:00
htmlToMarkdown : htmlToMarkdown ,
markupToHtml : markupToHtml ,
allAssets : allAssets ,
2023-07-16 18:42:42 +02:00
disabled : isReadOnly ,
2020-09-15 15:01:07 +02:00
themeId : props.themeId ,
2020-05-02 17:41:07 +02:00
dispatch : props.dispatch ,
2020-10-09 19:35:46 +02:00
noteToolbar : null ,
2020-05-02 17:41:07 +02:00
onScroll : onScroll ,
2020-07-23 00:13:23 +02:00
setLocalSearchResultCount : setLocalSearchResultCount ,
2020-05-02 17:41:07 +02:00
searchMarkers : searchMarkers ,
visiblePanes : props.noteVisiblePanes || [ 'editor' , 'viewer' ] ,
keyboardMode : Setting.value ( 'editor.keyboardMode' ) ,
2020-05-04 01:55:41 +02:00
locale : Setting.value ( 'locale' ) ,
2020-05-10 17:28:22 +02:00
onDrop : onDrop ,
2020-10-09 19:35:46 +02:00
noteToolbarButtonInfos : props.toolbarButtonInfos ,
plugins : props.plugins ,
2021-01-29 20:45:11 +02:00
fontSize : Setting.value ( 'style.editor.fontSize' ) ,
2021-08-14 13:19:53 +02:00
contentMaxWidth : props.contentMaxWidth ,
2021-10-30 18:51:19 +02:00
isSafeMode : props.isSafeMode ,
2023-01-17 14:08:22 +02:00
useCustomPdfViewer : props.useCustomPdfViewer ,
2022-08-27 14:32:20 +02:00
// We need it to identify the context for which media is rendered.
2024-02-26 12:16:23 +02:00
// It is currently used to remember pdf scroll position for each attachments of each note uniquely.
2022-08-27 14:32:20 +02:00
noteId : props.noteId ,
2024-04-03 19:29:22 +02:00
watchedNoteFiles : props.watchedNoteFiles ,
2020-05-02 17:41:07 +02:00
} ;
let editor = null ;
if ( props . bodyEditor === 'TinyMCE' ) {
editor = < TinyMCE { ...editorProps } / > ;
2023-10-22 13:00:19 +02:00
} else if ( props . bodyEditor === 'PlainText' ) {
editor = < PlainEditor { ...editorProps } / > ;
2020-06-06 17:00:20 +02:00
} else if ( props . bodyEditor === 'CodeMirror' ) {
2023-09-21 10:12:40 +02:00
editor = < CodeMirror5 { ...editorProps } / > ;
} else if ( props . bodyEditor === 'CodeMirror6' ) {
editor = < CodeMirror6 { ...editorProps } / > ;
2020-05-02 17:41:07 +02:00
} else {
throw new Error ( ` Invalid editor: ${ props . bodyEditor } ` ) ;
}
2020-12-09 17:58:15 +02:00
const onRichTextReadMoreLinkClick = useCallback ( ( ) = > {
2024-05-21 02:28:19 +02:00
void bridge ( ) . openExternal ( 'https://joplinapp.org/help/apps/rich_text_editor' ) ;
2020-12-09 17:58:15 +02:00
} , [ ] ) ;
const onRichTextDismissLinkClick = useCallback ( ( ) = > {
Setting . setValue ( 'richTextBannerDismissed' , true ) ;
} , [ ] ) ;
const wysiwygBanner = props . bodyEditor !== 'TinyMCE' || props . richTextBannerDismissed ? null : (
< div style = { styles . warningBanner } >
{ _ ( 'This Rich Text editor has a number of limitations and it is recommended to be aware of them before using it.' ) }
& nbsp ; & nbsp ; < a onClick = { onRichTextReadMoreLinkClick } style = { styles . warningBannerLink } href = "#" > [ { _ ( 'Read more about it' ) } ] < / a >
& nbsp ; & nbsp ; < a onClick = { onRichTextDismissLinkClick } style = { styles . warningBannerLink } href = "#" > [ { _ ( 'Dismiss' ) } ] < / a >
2020-05-02 17:41:07 +02:00
< / div >
) ;
const noteRevisionViewer_onBack = useCallback ( ( ) = > {
setShowRevisions ( false ) ;
} , [ ] ) ;
2023-12-13 21:24:58 +02:00
const onBannerResourceClick = useCallback ( async ( event : React.MouseEvent < HTMLAnchorElement > ) = > {
event . preventDefault ( ) ;
const resourceId = event . currentTarget . getAttribute ( 'data-resource-id' ) ;
await openItemById ( resourceId , props . dispatch ) ;
} , [ props . dispatch ] ) ;
2020-05-02 17:41:07 +02:00
if ( showRevisions ) {
2020-09-15 15:01:07 +02:00
const theme = themeStyle ( props . themeId ) ;
2020-05-02 17:41:07 +02:00
2024-05-21 02:28:19 +02:00
const revStyle : React.CSSProperties = {
2020-09-15 15:01:07 +02:00
// ...props.style,
2020-05-02 17:41:07 +02:00
display : 'inline-flex' ,
padding : theme.margin ,
verticalAlign : 'top' ,
boxSizing : 'border-box' ,
} ;
return (
< div style = { revStyle } >
< NoteRevisionViewer customCss = { props . customCss } noteId = { formNote . id } onBack = { noteRevisionViewer_onBack } / >
< / div >
) ;
}
if ( props . selectedNoteIds . length > 1 ) {
return < MultiNoteActions
2020-09-15 15:01:07 +02:00
themeId = { props . themeId }
2020-05-02 17:41:07 +02:00
selectedNoteIds = { props . selectedNoteIds }
notes = { props . notes }
dispatch = { props . dispatch }
watchedNoteFiles = { props . watchedNoteFiles }
2020-10-09 19:35:46 +02:00
plugins = { props . plugins }
2020-11-17 13:50:46 +02:00
inConflictFolder = { props . selectedFolderId === Folder . conflictFolderId ( ) }
2021-05-19 15:00:16 +02:00
customCss = { props . customCss }
2020-05-02 17:41:07 +02:00
/ > ;
}
function renderSearchBar() {
if ( ! showLocalSearch ) return false ;
2020-09-15 15:01:07 +02:00
const theme = themeStyle ( props . themeId ) ;
2020-05-02 17:41:07 +02:00
return (
< NoteSearchBar
ref = { noteSearchBarRef }
2022-11-14 18:48:41 +02:00
themeId = { props . themeId }
2020-05-02 17:41:07 +02:00
style = { {
display : 'flex' ,
height : 35 ,
borderTop : ` 1px solid ${ theme . dividerColor } ` ,
} }
query = { localSearch . query }
searching = { localSearch . searching }
resultCount = { localSearch . resultCount }
selectedIndex = { localSearch . selectedIndex }
onChange = { localSearch_change }
onNext = { localSearch_next }
onPrevious = { localSearch_previous }
onClose = { localSearch_close }
2020-09-06 17:28:23 +02:00
visiblePanes = { props . noteVisiblePanes }
2020-05-02 17:41:07 +02:00
/ >
) ;
}
2020-07-22 20:03:31 +02:00
function renderResourceWatchingNotification() {
if ( ! Object . keys ( props . watchedResources ) . length ) return null ;
const resourceTitles = Object . keys ( props . watchedResources ) . map ( id = > props . watchedResources [ id ] . title ) ;
return (
< div style = { styles . resourceWatchBanner } >
< p style = { styles . resourceWatchBannerLine } > { _ ( 'The following attachments are being watched for changes:' ) } < strong > { resourceTitles . join ( ', ' ) } < / strong > < / p >
< p style = { { . . . styles . resourceWatchBannerLine , marginBottom : 0 } } > { _ ( 'The attachments will no longer be watched when you switch to a different note.' ) } < / p >
< / div >
) ;
}
2023-12-13 21:24:58 +02:00
const renderResourceInSearchResultsNotification = ( ) = > {
const resourceResults = props . searchResults . filter ( r = > r . id === props . noteId && r . item_type === ModelType . Resource ) ;
if ( ! resourceResults . length ) return null ;
const renderResource = ( id : string , title : string ) = > {
return < li key = { id } > < a data - resource - id = { id } onClick = { onBannerResourceClick } href = "#" > { title } < / a > < / li > ;
} ;
return (
< div style = { styles . resourceWatchBanner } >
< p style = { styles . resourceWatchBannerLine } > { _n ( 'The following attachment matches your search query:' , 'The following attachments match your search query:' , resourceResults . length ) } < / p >
< ul >
{ resourceResults . map ( r = > renderResource ( r . item_id , r . title ) ) }
< / ul >
< / div >
) ;
} ;
2020-09-15 15:01:07 +02:00
function renderSearchInfo() {
2021-01-02 19:27:37 +02:00
const theme = themeStyle ( props . themeId ) ;
2020-09-15 15:01:07 +02:00
if ( formNoteFolder && [ 'Search' , 'Tag' , 'SmartFilter' ] . includes ( props . notesParentType ) ) {
return (
2021-01-02 19:27:37 +02:00
< div style = { { paddingTop : 10 , paddingBottom : 10 , paddingLeft : theme.editorPaddingLeft } } >
2020-09-15 15:01:07 +02:00
< Button
iconName = "icon-notebooks"
level = { ButtonLevel . Primary }
title = { _ ( 'In: %s' , substrWithEllipsis ( formNoteFolder . title , 0 , 100 ) ) }
onClick = { ( ) = > {
props . dispatch ( {
type : 'FOLDER_AND_NOTE_SELECT' ,
folderId : formNoteFolder.id ,
noteId : formNote.id ,
} ) ;
} }
/ >
< div style = { { flex : 1 } } > < / div >
< / div >
) ;
} else {
return null ;
}
}
2022-11-14 19:25:41 +02:00
if ( formNote . encryption_applied || ! formNote . id || ! effectiveNoteId ) {
2020-05-02 17:41:07 +02:00
return renderNoNotes ( styles . root ) ;
}
2021-01-02 19:27:37 +02:00
const theme = themeStyle ( props . themeId ) ;
2020-05-02 17:41:07 +02:00
return (
< div style = { styles . root } onDrop = { onDrop } >
< div style = { { display : 'flex' , flexDirection : 'column' , height : '100%' } } >
2020-07-22 20:03:31 +02:00
{ renderResourceWatchingNotification ( ) }
2023-12-13 21:24:58 +02:00
{ renderResourceInSearchResultsNotification ( ) }
2020-11-05 18:58:23 +02:00
< NoteTitleBar
titleInputRef = { titleInputRef }
themeId = { props . themeId }
isProvisional = { props . isProvisional }
noteIsTodo = { formNote . is_todo }
noteTitle = { formNote . title }
noteUserUpdatedTime = { formNote . user_updated_time }
onTitleChange = { onTitleChange }
2023-07-16 18:42:42 +02:00
disabled = { isReadOnly }
2020-11-05 18:58:23 +02:00
/ >
2020-09-15 15:01:07 +02:00
{ renderSearchInfo ( ) }
2023-09-21 10:12:40 +02:00
< div style = { { display : 'flex' , flex : 1 , paddingLeft : theme.editorPaddingLeft , maxHeight : '100%' , minHeight : '0' } } >
2020-05-02 17:41:07 +02:00
{ editor }
< / div >
< div style = { { display : 'flex' , flexDirection : 'row' , alignItems : 'center' } } >
{ renderSearchBar ( ) }
< / div >
2021-05-17 20:33:44 +02:00
< div className = "tag-bar" style = { { paddingLeft : theme.editorPaddingLeft , display : 'flex' , flexDirection : 'row' , alignItems : 'center' , height : 40 } } >
2020-09-15 15:01:07 +02:00
{ renderTagButton ( ) }
{ renderTagBar ( ) }
< / div >
2020-05-03 19:44:49 +02:00
{ wysiwygBanner }
2020-05-02 17:41:07 +02:00
< / div >
< / div >
) ;
}
export {
NoteEditor as NoteEditorComponent ,
} ;
2020-10-09 19:35:46 +02:00
const mapStateToProps = ( state : AppState ) = > {
2020-05-02 17:41:07 +02:00
const noteId = state . selectedNoteIds . length === 1 ? state . selectedNoteIds [ 0 ] : null ;
2020-10-18 22:52:10 +02:00
const whenClauseContext = stateToWhenClauseContext ( state ) ;
2020-05-02 17:41:07 +02:00
return {
noteId : noteId ,
notes : state.notes ,
selectedNoteIds : state.selectedNoteIds ,
2020-11-17 13:50:46 +02:00
selectedFolderId : state.selectedFolderId ,
2020-05-02 17:41:07 +02:00
isProvisional : state.provisionalNoteIds.includes ( noteId ) ,
editorNoteStatuses : state.editorNoteStatuses ,
syncStarted : state.syncStarted ,
2023-08-18 10:31:45 +02:00
decryptionStarted : state.decryptionWorker?.state !== 'idle' ,
2020-09-15 15:01:07 +02:00
themeId : state.settings.theme ,
2020-12-09 17:58:15 +02:00
richTextBannerDismissed : state.settings.richTextBannerDismissed ,
2020-05-02 17:41:07 +02:00
watchedNoteFiles : state.watchedNoteFiles ,
notesParentType : state.notesParentType ,
selectedNoteTags : state.selectedNoteTags ,
lastEditorScrollPercents : state.lastEditorScrollPercents ,
selectedNoteHash : state.selectedNoteHash ,
searches : state.searches ,
selectedSearchId : state.selectedSearchId ,
customCss : state.customCss ,
noteVisiblePanes : state.noteVisiblePanes ,
2020-07-22 20:03:31 +02:00
watchedResources : state.watchedResources ,
2020-09-06 14:07:00 +02:00
highlightedWords : state.highlightedWords ,
2020-10-09 19:35:46 +02:00
plugins : state.pluginService.plugins ,
2020-10-18 22:52:10 +02:00
toolbarButtonInfos : toolbarButtonUtils.commandsToToolbarButtons ( [
2020-10-09 19:35:46 +02:00
'historyBackward' ,
'historyForward' ,
'toggleEditors' ,
2020-10-10 14:32:30 +02:00
'toggleExternalEditing' ,
2020-10-18 22:52:10 +02:00
] , whenClauseContext ) ,
setTagsToolbarButtonInfo : toolbarButtonUtils.commandsToToolbarButtons ( [
2020-10-09 19:35:46 +02:00
'setTags' ,
2020-10-18 22:52:10 +02:00
] , whenClauseContext ) [ 0 ] ,
2021-08-14 13:19:53 +02:00
contentMaxWidth : state.settings [ 'style.editor.contentMaxWidth' ] ,
2021-10-30 18:51:19 +02:00
isSafeMode : state.settings.isSafeMode ,
2023-07-16 18:42:42 +02:00
useCustomPdfViewer : false ,
syncUserId : state.settings [ 'sync.userId' ] ,
2023-08-14 19:33:48 +02:00
shareCacheSetting : state.settings [ 'sync.shareCache' ] ,
2023-12-13 21:24:58 +02:00
searchResults : state.searchResults ,
2020-05-02 17:41:07 +02:00
} ;
} ;
export default connect ( mapStateToProps ) ( NoteEditor ) ;