From f71e6fe70397cf9ab2c109cad52780088337fc47 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Fri, 21 Oct 2022 11:49:23 +0530 Subject: [PATCH] fix: improve plaintext editor performance on mobile (#1855) --- .../NoteView/AutoresizingTextarea.tsx | 34 ------ .../Components/NoteView/NoteView.tsx | 103 +++++++----------- 2 files changed, 39 insertions(+), 98 deletions(-) delete mode 100644 packages/web/src/javascripts/Components/NoteView/AutoresizingTextarea.tsx diff --git a/packages/web/src/javascripts/Components/NoteView/AutoresizingTextarea.tsx b/packages/web/src/javascripts/Components/NoteView/AutoresizingTextarea.tsx deleted file mode 100644 index 07365ba3a..000000000 --- a/packages/web/src/javascripts/Components/NoteView/AutoresizingTextarea.tsx +++ /dev/null @@ -1,34 +0,0 @@ -import { classNames } from '@/Utils/ConcatenateClassNames' -import { ComponentPropsWithoutRef, ForwardedRef, forwardRef } from 'react' - -// Based on: https://css-tricks.com/auto-growing-inputs-textareas/#aa-other-ideas -const AutoresizingNoteViewTextarea = forwardRef( - ( - { value, className, ...textareaProps }: ComponentPropsWithoutRef<'textarea'>, - ref: ForwardedRef, - ) => { - return ( -
-
-          {value}{' '}
-        
- -
- ) - }, -) - -export default AutoresizingNoteViewTextarea diff --git a/packages/web/src/javascripts/Components/NoteView/NoteView.tsx b/packages/web/src/javascripts/Components/NoteView/NoteView.tsx index 3ca5186e4..502f43de1 100644 --- a/packages/web/src/javascripts/Components/NoteView/NoteView.tsx +++ b/packages/web/src/javascripts/Components/NoteView/NoteView.tsx @@ -13,12 +13,11 @@ import { NoteViewController, PayloadEmitSource, WebAppEvent, - Platform, EditorLineHeight, EditorFontSize, NoteType, } from '@standardnotes/snjs' -import { debounce, isDesktopApplication, isIOS } from '@/Utils' +import { debounce, isDesktopApplication } from '@/Utils' import { EditorEventSource } from '../../Types/EditorEventSource' import { confirmDialog, KeyboardModifier, KeyboardKey } from '@standardnotes/ui-services' import { STRING_DELETE_PLACEHOLDER_ATTEMPT, STRING_DELETE_LOCKED_ATTEMPT, StringDeleteNote } from '@/Constants/Strings' @@ -39,7 +38,6 @@ import { reloadFont } from './FontFunctions' import { NoteViewProps } from './NoteViewProps' import IndicatorCircle from '../IndicatorCircle/IndicatorCircle' import { classNames } from '@/Utils/ConcatenateClassNames' -import AutoresizingNoteViewTextarea from './AutoresizingTextarea' import MobileItemsListButton from '../NoteGroupView/MobileItemsListButton' import LinkedItemBubblesContainer from '../LinkedItems/LinkedItemBubblesContainer' import NoteStatusIndicator, { NoteStatus } from './NoteStatusIndicator' @@ -50,7 +48,6 @@ import NoteViewFileDropTarget from './NoteViewFileDropTarget' const MinimumStatusDuration = 400 const TextareaDebounce = 100 const NoteEditingDisabledText = 'Note editing disabled.' -const StickyHeaderScrollThresholdInPx = 20 function sortAlphabetically(array: SNComponent[]): SNComponent[] { return array.sort((a, b) => (a.name.toLowerCase() < b.name.toLowerCase() ? -1 : 1)) @@ -83,8 +80,6 @@ type State = { rightResizerWidth: number rightResizerOffset: number - shouldStickyHeader: boolean - monospaceFont?: boolean lineHeight?: EditorLineHeight fontSize?: EditorFontSize @@ -134,8 +129,6 @@ class NoteView extends PureComponent { this.textAreaChangeDebounceSave = debounce(this.textAreaChangeDebounceSave, TextareaDebounce) - this.handleWindowScroll = debounce(this.handleWindowScroll, 10) - this.state = { availableStackComponents: [], editorStateDidLoad: false, @@ -155,23 +148,18 @@ class NoteView extends PureComponent { leftResizerOffset: 0, rightResizerWidth: 0, rightResizerOffset: 0, - shouldStickyHeader: false, editorFeatureIdentifier: this.controller.item.editorIdentifier, noteType: this.controller.item.noteType, } this.noteViewElementRef = createRef() this.editorContentRef = createRef() - - window.addEventListener('scroll', this.handleWindowScroll) } override deinit() { super.deinit() ;(this.controller as unknown) = undefined - window.removeEventListener('scroll', this.handleWindowScroll) - this.removeComponentStreamObserver?.() ;(this.removeComponentStreamObserver as unknown) = undefined @@ -932,12 +920,6 @@ class NoteView extends PureComponent { } } - handleWindowScroll = () => { - this.setState({ - shouldStickyHeader: window.scrollY > StickyHeaderScrollThresholdInPx, - }) - } - override render() { if (this.state.showProtectedWarning || !this.application.isAuthorizedToRenderItem(this.note)) { return ( @@ -951,7 +933,11 @@ class NoteView extends PureComponent { } return ( -
+
{this.note && ( { {this.note && (
@@ -1017,36 +995,32 @@ class NoteView extends PureComponent { updateSavingIndicator={this.state.updateSavingIndicator} />
- {!this.state.shouldStickyHeader && ( -
- - - - -
- )} +
+ + + + +
- {!this.state.shouldStickyHeader && ( - - )} +
)} @@ -1082,12 +1056,8 @@ class NoteView extends PureComponent { )} {this.state.editorStateDidLoad && !this.state.editorComponentViewer && !this.state.textareaUnloading && ( - { ref={(ref) => ref && this.onSystemEditorLoad(ref)} spellCheck={this.state.spellcheck} value={this.state.editorText} - /> + className={classNames( + 'editable font-editor flex-grow', + this.state.lineHeight && `leading-${this.state.lineHeight.toLowerCase()}`, + this.state.fontSize && PlaintextFontSizeMapping[this.state.fontSize], + )} + > )} {this.state.marginResizersEnabled && this.editorContentRef.current ? (