feat: show selected editor icon in change editor button (#1615)

This commit is contained in:
Aman Harwara
2022-09-22 20:22:49 +05:30
committed by GitHub
parent cb7af03ce8
commit ce252def33
4 changed files with 35 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ import { FunctionComponent, useCallback, useRef, useState } from 'react'
import Icon from '@/Components/Icon/Icon' import Icon from '@/Components/Icon/Icon'
import ChangeEditorMenu from './ChangeEditorMenu' import ChangeEditorMenu from './ChangeEditorMenu'
import Popover from '../Popover/Popover' import Popover from '../Popover/Popover'
import { classNames } from '@/Utils/ConcatenateClassNames'
type Props = { type Props = {
application: WebApplication application: WebApplication
@@ -21,6 +22,12 @@ const ChangeEditorButton: FunctionComponent<Props> = ({
const [isOpen, setIsOpen] = useState(false) const [isOpen, setIsOpen] = useState(false)
const buttonRef = useRef<HTMLButtonElement>(null) const buttonRef = useRef<HTMLButtonElement>(null)
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const [selectedEditor, setSelectedEditor] = useState(() => {
return note ? application.componentManager.editorForNote(note) : undefined
})
const [selectedEditorIcon, selectedEditorIconTint] = application.iconsController.getIconAndTintForNoteType(
selectedEditor?.package_info.note_type,
)
const toggleMenu = useCallback(async () => { const toggleMenu = useCallback(async () => {
const willMenuOpen = !isOpen const willMenuOpen = !isOpen
@@ -33,13 +40,16 @@ const ChangeEditorButton: FunctionComponent<Props> = ({
return ( return (
<div ref={containerRef}> <div ref={containerRef}>
<button <button
className="bg-text-padding flex h-8 min-w-8 cursor-pointer items-center justify-center rounded-full border border-solid border-border text-neutral hover:bg-contrast focus:bg-contrast" className={classNames(
'bg-text-padding flex h-8 min-w-8 cursor-pointer items-center justify-center rounded-full border border-solid text-neutral hover:bg-contrast focus:bg-contrast',
`border-accessory-tint-${selectedEditorIconTint}`,
)}
title="Change note type" title="Change note type"
aria-label="Change note type" aria-label="Change note type"
onClick={toggleMenu} onClick={toggleMenu}
ref={buttonRef} ref={buttonRef}
> >
<Icon type="dashboard" /> <Icon type={selectedEditorIcon} className={`text-accessory-tint-${selectedEditorIconTint}`} />
</button> </button>
<Popover togglePopover={toggleMenu} anchorElement={buttonRef.current} open={isOpen} className="pt-2 md:pt-0"> <Popover togglePopover={toggleMenu} anchorElement={buttonRef.current} open={isOpen} className="pt-2 md:pt-0">
<ChangeEditorMenu <ChangeEditorMenu
@@ -49,6 +59,9 @@ const ChangeEditorButton: FunctionComponent<Props> = ({
closeMenu={() => { closeMenu={() => {
setIsOpen(false) setIsOpen(false)
}} }}
onSelect={(component) => {
setSelectedEditor(component)
}}
/> />
</Popover> </Popover>
</div> </div>

View File

@@ -31,11 +31,18 @@ type ChangeEditorMenuProps = {
closeMenu: () => void closeMenu: () => void
isVisible: boolean isVisible: boolean
note: SNNote | undefined note: SNNote | undefined
onSelect?: (component: SNComponent | undefined) => void
} }
const getGroupId = (group: EditorMenuGroup) => group.title.toLowerCase().replace(/\s/, '-') const getGroupId = (group: EditorMenuGroup) => group.title.toLowerCase().replace(/\s/, '-')
const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({ application, closeMenu, isVisible, note }) => { const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({
application,
closeMenu,
isVisible,
note,
onSelect,
}) => {
const editors = useMemo( const editors = useMemo(
() => () =>
application.componentManager.componentsForArea(ComponentArea.Editor).sort((a, b) => { application.componentManager.componentsForArea(ComponentArea.Editor).sort((a, b) => {
@@ -162,8 +169,12 @@ const ChangeEditorMenu: FunctionComponent<ChangeEditorMenuProps> = ({ applicatio
} }
closeMenu() closeMenu()
if (onSelect) {
onSelect(itemToBeSelected.component)
}
}, },
[application.componentManager, closeMenu, currentEditor, note, premiumModal, selectComponent], [application.componentManager, closeMenu, currentEditor, note, onSelect, premiumModal, selectComponent],
) )
return ( return (

View File

@@ -116,7 +116,7 @@ export class NotesController extends AbstractViewController {
return this.application.items.trashedItems.length return this.application.items.trashedItems.length
} }
setContextMenuOpen(open: boolean): void { setContextMenuOpen = (open: boolean) => {
this.contextMenuOpen = open this.contextMenuOpen = open
} }

View File

@@ -135,6 +135,12 @@ module.exports = {
'text-accessory-tint-4', 'text-accessory-tint-4',
'text-accessory-tint-5', 'text-accessory-tint-5',
'text-accessory-tint-6', 'text-accessory-tint-6',
'border-accessory-tint-1',
'border-accessory-tint-2',
'border-accessory-tint-3',
'border-accessory-tint-4',
'border-accessory-tint-5',
'border-accessory-tint-6',
], ],
plugins: [ plugins: [
plugin(function ({ addVariant }) { plugin(function ({ addVariant }) {