diff --git a/.eslintrc.js b/.eslintrc.js index ff85e0e..6c7c86d 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -34,13 +34,14 @@ module.exports = { "import/prefer-default-export": "off", "linebreak-style": "off", "max-len": "off", - "no-console": "off", - "no-new": "off", - "no-continue": "off", - "no-plusplus": "off", - "object-curly-newline": "off", "no-alert": "off", + "no-console": "off", + "no-continue": "off", + "no-new": "off", + "no-plusplus": "off", "no-restricted-globals": "off", + "object-curly-newline": "off", + "prefer-destructuring": "off", }, env: { browser: true, diff --git a/src/components/toolbar/Toolbar.ts b/src/components/toolbar/Toolbar.ts index de77049..b07bc79 100644 --- a/src/components/toolbar/Toolbar.ts +++ b/src/components/toolbar/Toolbar.ts @@ -3,7 +3,7 @@ import { $, Dom } from 'core/Dom'; import { ExcelComponentState } from 'core/ExcelComponentState'; import { ComponentOptionsType } from 'core/ExcelComponent'; import { createToolbar } from 'components/toolbar/toolbar.template'; -import { initialStyleState } from 'src/constants'; +import { fontSizes, initialStyleState } from 'src/constants'; export class Toolbar extends ExcelComponentState { static className = 'excel__toolbar'; @@ -44,11 +44,41 @@ export class Toolbar extends ExcelComponentState { onClick(event: MouseEvent) { const target = $(event.target as HTMLElement); - const stringValue = target?.data?.value; - if (!stringValue) return; - const value = JSON.parse(stringValue); - const key = Object.keys(value)[0]; + let stringValue; + let value; + let key; + + switch (true) { + case !!target.closest('[data-change-size]').$el: { + const el = target.closest('[data-change-size]'); + + if (el.hasClass('disable')) return; + + const currentSize = this.store.getState().currentStyles.fontSize; + let idx = fontSizes.findIndex(font => font === currentSize); + let nextSize; + + if (el.data.changeSize === 'increase') { + nextSize = fontSizes[++idx]; + } else if (el.data.changeSize === 'decrease') { + nextSize = fontSizes[--idx]; + } + + value = { fontSize: nextSize }; + key = 'fontSize'; + + break; + } + + default: { + stringValue = target?.data?.value; + if (!stringValue) return; + + value = JSON.parse(stringValue); + key = Object.keys(value)[0]; + } + } this.$emitEventToObserver('toolbar:applyStyle', value); this.setComponentState({ [key]: value[key] }); diff --git a/src/components/toolbar/toolbar.template.ts b/src/components/toolbar/toolbar.template.ts index 43763c7..2e1b179 100644 --- a/src/components/toolbar/toolbar.template.ts +++ b/src/components/toolbar/toolbar.template.ts @@ -1,5 +1,6 @@ import { ToolbarStateType } from 'components/toolbar/toolbar-types'; -import { initialStyleState } from 'src/constants'; +import { isLargestFontSize, isSmallestFontSize } from 'core/utils'; +import { fontFamilies, fontSizes, initialStyleState } from 'src/constants'; type ButtonConfigType = { icon: string; @@ -80,21 +81,38 @@ export function createToolbar(state: ToolbarStateType): string { ], ]; - const buttons = btns.map(btn => (Array.isArray(btn) ? toButtonGroup(btn) : toButton(btn))); + const buttons = btns.map(btn => (Array.isArray(btn) ? createButtonsFromConfigGroup(btn) : createButtonFromConfig(btn))); const selectGroup = createGroup(createFontSizeButton(state.fontSize), createFontFamilyButton(state.fontFamily)); + const increaseDecreaseFontSize = createSizeUpDownButtons(state.fontSize); + + buttons.push(increaseDecreaseFontSize); buttons.push(selectGroup); - return buttons.join(' '); + + return buttons.join(''); +} + +function createSizeUpDownButtons(currentSize = initialStyleState.fontSize) { + const buttons = ['increase', 'decrease']; + + return buttons.map(btn => { + const disable = btn === 'increase' ? isLargestFontSize(currentSize) : isSmallestFontSize(currentSize); + return ` +
+ `; + }).join(''); } function createFontSizeButton(currentSize = initialStyleState.fontSize) { - const fontSizeInPixels = +currentSize.slice(0, -2); - const options = []; - - for (let i = 8; i <= 24; i += 2) { - if (fontSizeInPixels === i) options.push(``); - else options.push(``); - } + const options = fontSizes.map(size => { + // remove 'px' part + const sizeValue = size.slice(0, -2); + return size === currentSize + ? `` + : ``; + }); return `