@react-page-plugins/slate-table
Version:
react-page plugin for slate table
172 lines • 6.29 kB
JavaScript
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
import { Editor, Transforms, Range, Point, Element as SlateElement } from 'slate';
var LIST_TYPES = ['numbered-list', 'bulleted-list'];
export var isBlockActive = function (editor, format) {
var _a = __read(Editor.nodes(editor, {
match: function (elem) {
if (Editor.isEditor(elem))
return false;
if (!SlateElement.isElement(elem))
return false;
return elem.type === format;
},
}), 1), match = _a[0];
return !!match;
};
export var isMarkActive = function (editor, format) {
var marks = Editor.marks(editor);
return marks ? marks[format] === true : false;
};
export var toggleBlock = function (editor, format) {
var isActive = isBlockActive(editor, format);
var isList = LIST_TYPES.includes(format);
Transforms.unwrapNodes(editor, {
match: function (elem) {
if (Editor.isEditor(elem))
return false;
if (!SlateElement.isElement(elem))
return false;
return LIST_TYPES.includes(elem.type);
},
split: true,
});
var newProperties = {
type: isActive ? 'paragraph' : isList ? 'list-item' : format,
};
Transforms.setNodes(editor, newProperties);
if (!isActive && isList) {
var block = { type: format, children: [] };
Transforms.wrapNodes(editor, block);
}
};
export var toggleMark = function (editor, format) {
var isActive = isMarkActive(editor, format);
if (isActive) {
Editor.removeMark(editor, format);
}
else {
Editor.addMark(editor, format, true);
}
};
export var countRowCol = function (table) {
var rcount = table.children.length;
var ccount = rcount > 0 ? table.children[0].children.length : 0;
return [rcount, ccount];
};
export var getContext = function (editor) {
var selection = editor.selection, data = editor.data;
var slate = data.slate;
var table = slate[0];
var _a = __read(countRowCol(table), 2), rcount = _a[0], ccount = _a[1];
var row;
var rowPath;
var cell;
var cellPath;
if (selection) {
var _b = __read(Editor.nodes(editor, {
match: function (n) { return !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'table-row'; },
}), 1), _row = _b[0];
var _c = __read(Editor.nodes(editor, {
match: function (n) { return !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'table-cell'; },
}), 1), _cell = _c[0];
if (_cell) {
cell = _cell;
var _d = __read(_cell, 2), _cellPath = _d[1];
cellPath = _cellPath;
}
if (_row) {
row = _row;
var _e = __read(_row, 2), _rowPath = _e[1];
rowPath = _rowPath;
}
}
return { selection: selection, rcount: rcount, ccount: ccount, cell: cell, cellPath: cellPath, row: row, rowPath: rowPath };
};
export var withTables = function (editor) {
var deleteBackward = editor.deleteBackward, deleteForward = editor.deleteForward, insertBreak = editor.insertBreak;
editor.addParagraph = function () {
var selection = editor.selection;
if (selection) {
var _a = __read(Editor.nodes(editor, {
match: function (n) {
return !Editor.isEditor(n) &&
SlateElement.isElement(n) &&
['paragraph', 'list-item', 'heading-two'].includes(n.type);
},
}), 1), cell = _a[0];
if (cell) {
var _b = __read(cell, 1), node = _b[0];
editor.insertNode({ type: node.type === 'list-item' ? 'list-item' : 'paragraph', children: [{ text: '' }] });
}
}
};
editor.deleteBackward = function (unit) {
var selection = editor.selection;
if (selection && Range.isCollapsed(selection)) {
var _a = __read(Editor.nodes(editor, {
match: function (n) { return !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'table-cell'; },
}), 1), cell = _a[0];
if (cell) {
var _b = __read(cell, 2), cellPath = _b[1];
var start = Editor.start(editor, cellPath);
if (Point.equals(selection.anchor, start)) {
return;
}
}
}
deleteBackward(unit);
};
editor.deleteForward = function (unit) {
var selection = editor.selection;
if (selection && Range.isCollapsed(selection)) {
var _a = __read(Editor.nodes(editor, {
match: function (n) { return !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'table-cell'; },
}), 1), cell = _a[0];
if (cell) {
var _b = __read(cell, 2), cellPath = _b[1];
var end = Editor.end(editor, cellPath);
if (Point.equals(selection.anchor, end)) {
return;
}
}
}
deleteForward(unit);
};
editor.insertBreak = function () {
var selection = editor.selection;
if (selection) {
var _a = __read(Editor.nodes(editor, {
match: function (n) { return !Editor.isEditor(n) && SlateElement.isElement(n) && n.type === 'table'; },
}), 1), table = _a[0];
if (table) {
return;
}
}
insertBreak();
};
return editor;
};
export default {
toggleBlock: toggleBlock,
toggleMark: toggleMark,
isBlockActive: isBlockActive,
isMarkActive: isMarkActive,
getContext: getContext,
withTables: withTables,
};
//# sourceMappingURL=core.js.map