UNPKG

@atlaskit/editor-plugin-text-color

Version:

Text color plugin for @atlaskit/editor-core

82 lines 3.87 kB
import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } import { SafePlugin } from '@atlaskit/editor-common/safe-plugin'; import { textColorPalette } from '@atlaskit/editor-common/ui-color'; import { PluginKey } from '@atlaskit/editor-prosemirror/state'; import { getActiveColor } from './utils/color'; import { DEFAULT_COLOR } from './utils/constants'; import { getDisabledState } from './utils/disabled'; function createInitialPluginState(editorState, pluginConfig) { var defaultColor = (pluginConfig === null || pluginConfig === void 0 ? void 0 : pluginConfig.defaultColor) || DEFAULT_COLOR; var palette = [{ value: defaultColor.color, label: defaultColor.label, border: "var(--ds-border, #0B120E24)" }].concat(_toConsumableArray(textColorPalette)); var state = { color: getActiveColor(editorState), disabled: getDisabledState(editorState), palette: palette, defaultColor: defaultColor.color, isPaletteOpen: false }; return state; } export var ACTIONS = /*#__PURE__*/function (ACTIONS) { ACTIONS[ACTIONS["RESET_COLOR"] = 0] = "RESET_COLOR"; ACTIONS[ACTIONS["SET_COLOR"] = 1] = "SET_COLOR"; ACTIONS[ACTIONS["DISABLE"] = 2] = "DISABLE"; ACTIONS[ACTIONS["SET_PALETTE"] = 3] = "SET_PALETTE"; return ACTIONS; }({}); export var pluginKey = new PluginKey('textColorPlugin'); export function createPlugin(dispatch, pluginConfig) { return new SafePlugin({ key: pluginKey, state: { init: function init(_config, editorState) { return createInitialPluginState(editorState, pluginConfig); }, apply: function apply(tr, pluginState, _, newState) { var meta = tr.getMeta(pluginKey) || {}; var nextState; switch (meta.action) { case ACTIONS.RESET_COLOR: nextState = _objectSpread(_objectSpread({}, pluginState), {}, { color: pluginState.defaultColor }); break; case ACTIONS.SET_COLOR: nextState = _objectSpread(_objectSpread({}, pluginState), {}, { color: meta.color, disabled: false, isPaletteOpen: false }); break; case ACTIONS.DISABLE: nextState = _objectSpread(_objectSpread({}, pluginState), {}, { disabled: true }); break; case ACTIONS.SET_PALETTE: nextState = _objectSpread(_objectSpread({}, pluginState), {}, { isPaletteOpen: meta.isPaletteOpen }); break; default: nextState = _objectSpread(_objectSpread({}, pluginState), {}, { color: getActiveColor(newState), disabled: getDisabledState(newState) }); } if (pluginState && pluginState.color !== nextState.color || pluginState && pluginState.disabled !== nextState.disabled || pluginState && pluginState.isPaletteOpen !== nextState.isPaletteOpen) { dispatch(pluginKey, nextState); return nextState; } return pluginState; } } }); }