UNPKG

@atlaskit/editor-plugin-paste-options-toolbar

Version:

Paste options toolbar for @atlaskit/editor-core

177 lines (165 loc) 6.3 kB
import { hasMixedNodes } from './hasMixedNodes'; import { hasTableNode } from './hasTableNode'; import { isNotProse } from './isNotProse'; import { isNotSingleLink } from './isNotSingleLink'; /** * Returns a rule that hides the paste-menu button when the pasted plain-text * is shorter than `minChars` characters. */ var _minCharsRule = function minCharsRule(minChars) { return function (context) { return context.getPlaintextLength() < minChars; }; }; /** * Returns a rule that hides the paste-menu button when the pasted plain-text * is longer than `maxChars` characters. */ var _maxCharsRule = function maxCharsRule(maxChars) { return function (context) { return context.getPlaintextLength() > maxChars; }; }; /** * A rule that hides the paste-menu button when the pasted content is plain * text (i.e. not rich-text / prose). */ var notProseRule = function notProseRule(context) { // The pasted slice being empty or missing indicates plain-text paste. var slice = context.getPastedSlice(); var pastedText = context.getPastedText(); return slice === undefined && isNotProse(pastedText); }; /** * A rule that hides the paste-menu button when the pasted content contains a * table node as an ancestor at the insertion point. */ var containsTableRule = function containsTableRule(context) { return hasTableNode(context.getPastedSlice()); }; /** * Returns a rule that hides the paste-menu button when any of the provided * node names appear in the ancestor list at the insertion point. */ var _excludedAncestorRule = function excludedAncestorRule(excludedNames) { return function (context) { return context.getAncestorNodeNames().some(function (name) { return excludedNames.includes(name); }); }; }; /** * Returns a rule that hides the paste-menu button when the pasted content contains * more than one node type (e.g. a mix of paragraphs and headings, or a table * with multiple cell types). */ var hideIfMixedNodesRule = function hideIfMixedNodesRule(context) { var slice = context.getPastedSlice(); if (!slice) { return false; } return hasMixedNodes(slice); }; /** * Returns a rule that hides the paste-menu button when the pasted content contains * only a single node type (e.g. a single paragraph, or a table with only one cell type). * This is the inverse of `hideIfMixedNodesRule` and can be used in combination with it * to target pastes that contain either exactly one node type or more than one node type. */ var hideIfSingleNodeRule = function hideIfSingleNodeRule(context) { var slice = context.getPastedSlice(); if (!slice) { return false; } return !hasMixedNodes(slice); }; /** * A rule that hides the paste-menu button when the paste source is NOT an * external application (i.e. the content was pasted from within the Fabric * editor or renderer rather than from a third-party source). */ var notExternalPasteRule = function notExternalPasteRule(context) { var source = context.getPasteSource(); return source === 'fabric-editor' || source === 'fabric-renderer'; }; /** * A rule that hides the paste-menu button when the pasted content is a single * standalone link — i.e. a bare URL link (text equals href) alone in a * paragraph, or a single inline card (smartlink) alone in a paragraph. * * Returns `true` (hidden) when the paste is NOT a single link — e.g. a link * with a custom label, a link accompanied by other text or sibling nodes, or * multiple paragraphs. */ var notSingleLinkRule = function notSingleLinkRule(context) { return isNotSingleLink(context.getPastedSlice()); }; /** * A rule that hides the paste-menu button when the pasted content IS a single * standalone link (the inverse of `notSingleLinkRule`). * * Use this to suppress buttons (e.g. Improve Writing, Fix Spelling) that * should not appear for single-link pastes. */ var isSingleLinkRule = function isSingleLinkRule(context) { return !isNotSingleLink(context.getPastedSlice()); }; /** * Combines multiple context-aware rules with short-circuit evaluation. * Returns `true` (hidden) as soon as the first rule returns `true`; returns * `false` only when all rules return `false`. * Context is injected by `createPasteMenuRuleFactories` — callers never * construct or pass a context object. */ var _allRules = function allRules() { for (var _len = arguments.length, rules = new Array(_len), _key = 0; _key < _len; _key++) { rules[_key] = arguments[_key]; } return function (context) { for (var _i = 0, _rules = rules; _i < _rules.length; _i++) { var rule = _rules[_i]; if (rule(context)) { return true; } } return false; }; }; /** * Binds a `PasteMenuRuleWithContext` to a context supplier, returning a * no-argument `PasteMenuRule` that re-reads the context on every invocation. */ var bind = function bind(getContext, rule) { return function () { return rule(getContext()); }; }; /** * Creates the `PasteMenuRuleFactories` object with every rule pre-bound to the * given context supplier. Called inside `getPasteMenuRules()` so the plugin * API and paste state are already available in the closure — external callers * never need to construct or pass a context object. */ export var createPasteMenuRuleFactories = function createPasteMenuRuleFactories(getContext) { return { allRules: function allRules() { return bind(getContext, _allRules.apply(void 0, arguments)); }, containsTableRule: bind(getContext, containsTableRule), excludedAncestorRule: function excludedAncestorRule(excludedNames) { return bind(getContext, _excludedAncestorRule(excludedNames)); }, hideIfMixedNodesRule: bind(getContext, hideIfMixedNodesRule), hideIfSingleNodeRule: bind(getContext, hideIfSingleNodeRule), isSingleLinkRule: bind(getContext, isSingleLinkRule), maxCharsRule: function maxCharsRule(maxChars) { return bind(getContext, _maxCharsRule(maxChars)); }, minCharsRule: function minCharsRule(minChars) { return bind(getContext, _minCharsRule(minChars)); }, notExternalPasteRule: bind(getContext, notExternalPasteRule), notProseRule: bind(getContext, notProseRule), notSingleLinkRule: bind(getContext, notSingleLinkRule) }; };