@atlaskit/editor-plugin-text-formatting
Version:
Text-formatting plugin for @atlaskit/editor-core
206 lines (204 loc) • 9.03 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FORMATTING_NODE_TYPES = exports.FORMATTING_MARK_TYPES = void 0;
exports.clearFormatting = clearFormatting;
exports.clearFormattingWithAnalytics = clearFormattingWithAnalytics;
exports.clearFormattingWithAnalyticsNext = void 0;
var _analytics = require("@atlaskit/editor-common/analytics");
var _transform = require("@atlaskit/editor-prosemirror/transform");
var _cellSelection = require("@atlaskit/editor-tables/cell-selection");
var _cellSelection2 = require("./utils/cell-selection");
var FORMATTING_NODE_TYPES = exports.FORMATTING_NODE_TYPES = ['heading', 'blockquote'];
var FORMATTING_MARK_TYPES = exports.FORMATTING_MARK_TYPES = ['em', 'code', 'strike', 'strong', 'underline', 'textColor', 'subsup', 'backgroundColor'];
var formatTypes = {
em: _analytics.ACTION_SUBJECT_ID.FORMAT_ITALIC,
code: _analytics.ACTION_SUBJECT_ID.FORMAT_CODE,
strike: _analytics.ACTION_SUBJECT_ID.FORMAT_STRIKE,
strong: _analytics.ACTION_SUBJECT_ID.FORMAT_STRONG,
underline: _analytics.ACTION_SUBJECT_ID.FORMAT_UNDERLINE,
textColor: _analytics.ACTION_SUBJECT_ID.FORMAT_COLOR,
subsup: 'subsup',
backgroundColor: _analytics.ACTION_SUBJECT_ID.FORMAT_BACKGROUND_COLOR
};
// eslint-disable-next-line @repo/internal/deprecations/deprecation-ticket-required
/**
* Consider removing this function when cleaning up platform_editor–toolbar_aifc
* @deprecated use `clearFormattingWithAnalyticsNext` instead, which returns EditorCommand
*/
function clearFormattingWithAnalytics(inputMethod, editorAnalyticsAPI) {
return clearFormatting(inputMethod, editorAnalyticsAPI);
}
var clearNodeFormattingOnSelectionNext = function clearNodeFormattingOnSelectionNext(schema, tr, formattedNodeType, nodeName, formattingCleared) {
return function (node, pos) {
if (node.type === formattedNodeType) {
if (formattedNodeType.isTextblock) {
tr.setNodeMarkup(pos, schema.nodes.paragraph);
formattingCleared.push(nodeName);
return false;
} else {
// In case of panel or blockquote
var fromPos = tr.doc.resolve(pos + 1);
var toPos = tr.doc.resolve(pos + node.nodeSize - 1);
var nodeRange = fromPos.blockRange(toPos);
if (nodeRange) {
var targetLiftDepth = (0, _transform.liftTarget)(nodeRange);
if (targetLiftDepth || targetLiftDepth === 0) {
formattingCleared.push(nodeName);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
tr.lift(nodeRange, targetLiftDepth);
}
}
}
}
return true;
};
};
function clearNodeFormattingOnSelection(state, tr, formattedNodeType, nodeName, formattingCleared) {
return function (node, pos) {
if (node.type === formattedNodeType) {
if (formattedNodeType.isTextblock) {
tr.setNodeMarkup(pos, state.schema.nodes.paragraph);
formattingCleared.push(nodeName);
return false;
} else {
// In case of panel or blockquote
var fromPos = tr.doc.resolve(pos + 1);
var toPos = tr.doc.resolve(pos + node.nodeSize - 1);
var nodeRange = fromPos.blockRange(toPos);
if (nodeRange) {
var targetLiftDepth = (0, _transform.liftTarget)(nodeRange);
if (targetLiftDepth || targetLiftDepth === 0) {
formattingCleared.push(nodeName);
// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
tr.lift(nodeRange, targetLiftDepth);
}
}
}
}
return true;
};
}
function clearFormatting(inputMethod, editorAnalyticsAPI) {
return function (state, dispatch) {
var tr = state.tr;
var formattingCleared = [];
FORMATTING_MARK_TYPES.forEach(function (mark) {
var _tr$selection = tr.selection,
from = _tr$selection.from,
to = _tr$selection.to;
var markType = state.schema.marks[mark];
if (!markType) {
return;
}
if (tr.selection instanceof _cellSelection.CellSelection) {
(0, _cellSelection2.cellSelectionNodesBetween)(tr.selection, tr.doc, function (node, pos) {
var isTableCell = node.type === state.schema.nodes.tableCell || node.type === state.schema.nodes.tableHeader;
if (!isTableCell) {
return true;
}
if (tr.doc.rangeHasMark(pos, pos + node.nodeSize, markType)) {
formattingCleared.push(formatTypes[mark]);
tr.removeMark(pos, pos + node.nodeSize, markType);
}
return false;
});
} else if (tr.doc.rangeHasMark(from, to, markType)) {
formattingCleared.push(formatTypes[mark]);
tr.removeMark(from, to, markType);
}
});
FORMATTING_NODE_TYPES.forEach(function (nodeName) {
var formattedNodeType = state.schema.nodes[nodeName];
var _tr$selection2 = tr.selection,
$from = _tr$selection2.$from,
$to = _tr$selection2.$to;
if (tr.selection instanceof _cellSelection.CellSelection) {
(0, _cellSelection2.cellSelectionNodesBetween)(tr.selection, tr.doc, clearNodeFormattingOnSelection(state, tr, formattedNodeType, nodeName, formattingCleared));
} else {
tr.doc.nodesBetween($from.pos, $to.pos, clearNodeFormattingOnSelection(state, tr, formattedNodeType, nodeName, formattingCleared));
}
});
tr.setStoredMarks([]);
if (formattingCleared.length && inputMethod) {
editorAnalyticsAPI === null || editorAnalyticsAPI === void 0 || editorAnalyticsAPI.attachAnalyticsEvent({
action: _analytics.ACTION.FORMATTED,
eventType: _analytics.EVENT_TYPE.TRACK,
actionSubject: _analytics.ACTION_SUBJECT.TEXT,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.FORMAT_CLEAR,
attributes: {
inputMethod: inputMethod,
formattingCleared: formattingCleared,
dropdownMenu: inputMethod === _analytics.INPUT_METHOD.TOOLBAR || inputMethod === _analytics.INPUT_METHOD.FLOATING_TB ? 'textFormatting' : undefined
}
})(tr);
}
if (dispatch) {
dispatch(tr);
}
return true;
};
}
var clearFormattingWithAnalyticsNext = exports.clearFormattingWithAnalyticsNext = function clearFormattingWithAnalyticsNext(editorAnalyticsApi) {
return function (inputMethod) {
return function (_ref) {
var tr = _ref.tr;
var formattingCleared = [];
var schema = tr.doc.type.schema;
FORMATTING_MARK_TYPES.forEach(function (mark) {
var _tr$selection3 = tr.selection,
from = _tr$selection3.from,
to = _tr$selection3.to;
var markType = schema.marks[mark];
if (!markType) {
return;
}
if (tr.selection instanceof _cellSelection.CellSelection) {
(0, _cellSelection2.cellSelectionNodesBetween)(tr.selection, tr.doc, function (node, pos) {
var isTableCell = node.type === tr.doc.type.schema.nodes.tableCell || node.type === schema.nodes.tableHeader;
if (!isTableCell) {
return true;
}
if (tr.doc.rangeHasMark(pos, pos + node.nodeSize, markType)) {
formattingCleared.push(formatTypes[mark]);
tr.removeMark(pos, pos + node.nodeSize, markType);
}
return false;
});
} else if (tr.doc.rangeHasMark(from, to, markType)) {
formattingCleared.push(formatTypes[mark]);
tr.removeMark(from, to, markType);
}
});
FORMATTING_NODE_TYPES.forEach(function (nodeName) {
var formattedNodeType = schema.nodes[nodeName];
var _tr$selection4 = tr.selection,
$from = _tr$selection4.$from,
$to = _tr$selection4.$to;
if (tr.selection instanceof _cellSelection.CellSelection) {
(0, _cellSelection2.cellSelectionNodesBetween)(tr.selection, tr.doc, clearNodeFormattingOnSelectionNext(schema, tr, formattedNodeType, nodeName, formattingCleared));
} else {
tr.doc.nodesBetween($from.pos, $to.pos, clearNodeFormattingOnSelectionNext(schema, tr, formattedNodeType, nodeName, formattingCleared));
}
});
tr.setStoredMarks([]);
if (formattingCleared.length && inputMethod) {
editorAnalyticsApi === null || editorAnalyticsApi === void 0 || editorAnalyticsApi.attachAnalyticsEvent({
action: _analytics.ACTION.FORMATTED,
eventType: _analytics.EVENT_TYPE.TRACK,
actionSubject: _analytics.ACTION_SUBJECT.TEXT,
actionSubjectId: _analytics.ACTION_SUBJECT_ID.FORMAT_CLEAR,
attributes: {
inputMethod: inputMethod,
formattingCleared: formattingCleared,
dropdownMenu: inputMethod === _analytics.INPUT_METHOD.TOOLBAR || inputMethod === _analytics.INPUT_METHOD.FLOATING_TB ? 'textFormatting' : undefined
}
})(tr);
}
return tr;
};
};
};