editor.js/src/components/tools/block.ts
Tanya 6c0555a322
[Feature] Multiple toolbox items for single tool (#2050)
* the popover component, vertical toolbox

* toolbox position improved

* popover width improved

* always show the plus button

* search field added

* search input in popover

* trying to create mobile toolbox

* FIx mobile popover fixed positioning

* Add mobile popover overlay

* Hide mobile popover on scroll

* Tmp

* feat(toolbox): popover adapted for mobile devices (#2004)

* FIx mobile popover fixed positioning

* Add mobile popover overlay

* Hide mobile popover on scroll

* Alter toolbox buttons hover

* Fix closing popover on overlay click

* Tests fix

* Fix onchange test

* restore focus after toolbox closing by ESC

* don't move toolbar by block-hover on mobile

Resolves #1972

* popover mobile styles improved

* Cleanup

* Remove scroll event listener

* Lock scroll on mobile

* don't show shortcuts in mobile popover

* Change data attr name

* Remove unused styles

* Remove unused listeners

* disable hover on mobile popover

* Scroll fix

* Lint

* Revert "Scroll fix"

This reverts commit 82deae543e.

* Return back background color for active state of toolbox buttons

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>

* Vertical toolbox fixes (#2017)

* Replace visibility property with display for hiding popover

* Disable arrow right and left keys for popover

* Revert "Replace visibility property with display for hiding popover"

This reverts commit af521cf6f2.

* Hide popover via setting max-height to 0 to fix animation in safari

* Remove redundant condition

* Extend element interface to avoid ts errors

* Do not subscribe to block hovered if mobile

* Add unsubscribing from overlay click event

* Rename isMobile to isMobileScreen

* Cleanup

* fix: popover opening direction (#2022)

* Change popover opening direction based on available space below it

* Update check

* Use cacheable decorator

* Update src/components/flipper.ts

Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com>

* Fixes

* Fix test

* Clear search on popover hide

* Fix popover width

* Fix for tests

* Update todos

* Linter fixes

* rm todo about beforeInsert

because I have no idea what does it mean

* i18n for search labels done

* rm methods for hiding/showing of +

* some code style update

* Update CHANGELOG.md

* make the list items a little bit compact

* fix z-index issue caused by block-appearing animation

also, improve popover padding for two reasons:

- make the popover more consistent with the Table tool popover (in future, it can be done with the same api method)
- make popover looks better

* Some progress

Use overriden config

tmp

* Cleanup

* Proceed cleanup

* Update tool-settings.d.ts

* Get rid of isToolboxItemActive

* Get rid of key

* Filter out duplicates in conversion menu

* Rename hash to id

* Change function for generating hash

* Cleanup

* Further cleanup

* [Feature] Multiple toolbox items: using of data overrides instead of config overrides (#2064)

* Use data instead of config

* check if active toolbox entry exists

* comparison improved

* eslint fix

* rename toolbox types, simplify hasTools method

* add empty line

* wrong line

* add multiple toobox note to the doc

* Update toolbox configs merge logic

* Add a test case

* Add toolbox ui tests

* Update tests

* upd doc

* Update header

* Update changelog and package.json

* Update changelog

* Update jsdoc

* Remove unused dependency

* Make BlockTool's toolbox getter always return an array

* Fix for unconfigured toolbox

* Revert "Fix for unconfigured toolbox"

This reverts commit dff1df2304.

* Change return type

* Merge data overrides with actual block data when inserting a block

* Revert "Merge data overrides with actual block data when inserting a block"

This reverts commit eb0a59cc64.

* Merge tool's data with data overrides

* Move merging block data with data overrides to insertNewBlock

* Update changelog

* Rename getDefaultBlockData to composeBlockData

* Create block data on condition

* Update types/api/blocks.d.ts

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>

* Update src/components/modules/api/blocks.ts

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>

Co-authored-by: Peter Savchenko <specc.dev@gmail.com>
Co-authored-by: George Berezhnoy <gohabereg@users.noreply.github.com>
2022-06-17 18:31:55 +03:00

216 lines
5.7 KiB
TypeScript

import BaseTool, { InternalBlockToolSettings, ToolType, UserSettings } from './base';
import {
BlockAPI,
BlockTool as IBlockTool,
BlockToolConstructable,
BlockToolData,
ConversionConfig,
PasteConfig, SanitizerConfig, ToolboxConfig,
ToolboxConfigEntry
} from '../../../types';
import * as _ from '../utils';
import InlineTool from './inline';
import BlockTune from './tune';
import ToolsCollection from './collection';
/**
* Class to work with Block tools constructables
*/
export default class BlockTool extends BaseTool<IBlockTool> {
/**
* Tool type — Block
*/
public type = ToolType.Block;
/**
* InlineTool collection for current Block Tool
*/
public inlineTools: ToolsCollection<InlineTool> = new ToolsCollection<InlineTool>();
/**
* BlockTune collection for current Block Tool
*/
public tunes: ToolsCollection<BlockTune> = new ToolsCollection<BlockTune>();
/**
* Tool's constructable blueprint
*/
protected constructable: BlockToolConstructable;
/**
* Creates new Tool instance
*
* @param data - Tool data
* @param block - BlockAPI for current Block
* @param readOnly - True if Editor is in read-only mode
*/
public create(data: BlockToolData, block: BlockAPI, readOnly: boolean): IBlockTool {
// eslint-disable-next-line new-cap
return new this.constructable({
data,
block,
readOnly,
api: this.api.getMethodsForTool(this),
config: this.settings,
}) as IBlockTool;
}
/**
* Returns true if read-only mode is supported by Tool
*/
public get isReadOnlySupported(): boolean {
return this.constructable[InternalBlockToolSettings.IsReadOnlySupported] === true;
}
/**
* Returns true if Tool supports linebreaks
*/
public get isLineBreaksEnabled(): boolean {
return this.constructable[InternalBlockToolSettings.IsEnabledLineBreaks];
}
/**
* Returns Tool toolbox configuration (internal or user-specified).
*
* Merges internal and user-defined toolbox configs based on the following rules:
*
* - If both internal and user-defined toolbox configs are arrays their items are merged.
* Length of the second one is kept.
*
* - If both are objects their properties are merged.
*
* - If one is an object and another is an array than internal config is replaced with user-defined
* config. This is made to allow user to override default tool's toolbox representation (single/multiple entries)
*/
public get toolbox(): ToolboxConfigEntry[] | undefined {
const toolToolboxSettings = this.constructable[InternalBlockToolSettings.Toolbox] as ToolboxConfig;
const userToolboxSettings = this.config[UserSettings.Toolbox];
if (_.isEmpty(toolToolboxSettings)) {
return;
}
if (userToolboxSettings === false) {
return;
}
/**
* Return tool's toolbox settings if user settings are not defined
*/
if (!userToolboxSettings) {
return Array.isArray(toolToolboxSettings) ? toolToolboxSettings : [ toolToolboxSettings ];
}
/**
* Otherwise merge user settings with tool's settings
*/
if (Array.isArray(toolToolboxSettings)) {
if (Array.isArray(userToolboxSettings)) {
return userToolboxSettings.map((item, i) => {
const toolToolboxEntry = toolToolboxSettings[i];
if (toolToolboxEntry) {
return {
...toolToolboxEntry,
...item,
};
}
return item;
});
}
return [ userToolboxSettings ];
} else {
if (Array.isArray(userToolboxSettings)) {
return userToolboxSettings;
}
return [
{
...toolToolboxSettings,
...userToolboxSettings,
},
];
}
}
/**
* Returns Tool conversion configuration
*/
public get conversionConfig(): ConversionConfig {
return this.constructable[InternalBlockToolSettings.ConversionConfig];
}
/**
* Returns enabled inline tools for Tool
*/
public get enabledInlineTools(): boolean | string[] {
return this.config[UserSettings.EnabledInlineTools] || false;
}
/**
* Returns enabled tunes for Tool
*/
public get enabledBlockTunes(): boolean | string[] {
return this.config[UserSettings.EnabledBlockTunes];
}
/**
* Returns Tool paste configuration
*/
public get pasteConfig(): PasteConfig {
return this.constructable[InternalBlockToolSettings.PasteConfig] || {};
}
/**
* Returns sanitize configuration for Block Tool including configs from related Inline Tools and Block Tunes
*/
@_.cacheable
public get sanitizeConfig(): SanitizerConfig {
const toolRules = super.sanitizeConfig;
const baseConfig = this.baseSanitizeConfig;
if (_.isEmpty(toolRules)) {
return baseConfig;
}
const toolConfig = {} as SanitizerConfig;
for (const fieldName in toolRules) {
if (Object.prototype.hasOwnProperty.call(toolRules, fieldName)) {
const rule = toolRules[fieldName];
/**
* If rule is object, merge it with Inline Tools configuration
*
* Otherwise pass as it is
*/
if (_.isObject(rule)) {
toolConfig[fieldName] = Object.assign({}, baseConfig, rule);
} else {
toolConfig[fieldName] = rule;
}
}
}
return toolConfig;
}
/**
* Returns sanitizer configuration composed from sanitize config of Inline Tools enabled for Tool
*/
@_.cacheable
public get baseSanitizeConfig(): SanitizerConfig {
const baseConfig = {};
Array
.from(this.inlineTools.values())
.forEach(tool => Object.assign(baseConfig, tool.sanitizeConfig));
Array
.from(this.tunes.values())
.forEach(tune => Object.assign(baseConfig, tune.sanitizeConfig));
return baseConfig;
}
}