@yuntijs/ui
Version:
☁️ Yunti UI - an open-source UI component library for building Cloud Native web apps
140 lines • 6.59 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
import { $createListItemNode, $createListNode, $isListItemNode, $isListNode, ListItemNode, ListNode } from '@lexical/list';
import { $getState, $setState, createState } from 'lexical';
var ORDERED_LIST_REGEX = /^(\s*)(\d+)\.\s/;
var UNORDERED_LIST_REGEX = /^(\s*)[*+-]\s/;
var CHECK_LIST_REGEX = /^(\s*)(?:[*+-]\s)?\s?(\[(\s|x)?])\s/i;
var listMarkerState = createState('mdListMarker', {
parse: function parse(v) {
return typeof v === 'string' && /^[*+-]$/.test(v) ? v : '-';
}
});
var LIST_INDENT_SIZE = 2;
function getIndent(whitespaces) {
var tabs = whitespaces.match(/\t/g);
var spaces = whitespaces.match(/ /g);
var indent = 0;
if (tabs) {
indent += tabs.length;
}
if (spaces) {
indent += Math.floor(spaces.length / LIST_INDENT_SIZE);
}
return indent;
}
var listReplace = function listReplace(listType) {
return function (parentNode, children, match, isImport) {
var previousNode = parentNode.getPreviousSibling();
var nextNode = parentNode.getNextSibling();
var listItem = $createListItemNode(listType === 'check' ? match[3] === 'x' : undefined);
var firstMatchChar = match[0].trim()[0];
var listMarker = (listType === 'bullet' || listType === 'check') && firstMatchChar === listMarkerState.parse(firstMatchChar) ? firstMatchChar : undefined;
if ($isListNode(nextNode) && nextNode.getListType() === listType) {
if (listMarker) {
$setState(nextNode, listMarkerState, listMarker);
}
var firstChild = nextNode.getFirstChild();
if (firstChild === null) {
// should never happen, but let's handle gracefully, just in case.
nextNode.append(listItem);
} else {
firstChild.insertBefore(listItem);
}
parentNode.remove();
} else if ($isListNode(previousNode) && previousNode.getListType() === listType) {
if (listMarker) {
$setState(previousNode, listMarkerState, listMarker);
}
previousNode.append(listItem);
parentNode.remove();
} else {
var list = $createListNode(listType, listType === 'number' ? Number(match[2]) : undefined);
if (listMarker) {
$setState(list, listMarkerState, listMarker);
}
list.append(listItem);
parentNode.replace(list);
}
listItem.append.apply(listItem, _toConsumableArray(children));
if (!isImport) {
listItem.select(0, 0);
}
var indent = getIndent(match[1]);
if (indent) {
listItem.setIndent(indent);
}
};
};
var $listExport = function $listExport(listNode, exportChildren, depth) {
var output = [];
var children = listNode.getChildren();
var index = 0;
var _iterator = _createForOfIteratorHelper(children),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var listItemNode = _step.value;
if ($isListItemNode(listItemNode)) {
if (listItemNode.getChildrenSize() === 1) {
var firstChild = listItemNode.getFirstChild();
if ($isListNode(firstChild)) {
output.push($listExport(firstChild, exportChildren, depth + 1));
continue;
}
}
var indent = ' '.repeat(depth * LIST_INDENT_SIZE);
var listType = listNode.getListType();
var listMarker = $getState(listNode, listMarkerState);
var prefix = listMarker + ' ';
if (listType === 'number') {
prefix = "".concat(listNode.getStart() + index, ". ");
} else if (listType === 'check') {
prefix = "".concat(listMarker, " [").concat(listItemNode.getChecked() ? 'x' : ' ', "] ");
}
// const prefix =
// listType === 'number'
// ? `${listNode.getStart() + index}. `
// : listType === 'check'
// ? `${listMarker} [${listItemNode.getChecked() ? 'x' : ' '}] `
// : listMarker + ' ';
output.push(indent + prefix + exportChildren(listItemNode));
index++;
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return output.join('\n');
};
export var UNORDERED_LIST = {
dependencies: [ListNode, ListItemNode],
export: function _export(node, exportChildren) {
return $isListNode(node) ? $listExport(node, exportChildren, 0) : null;
},
regExp: UNORDERED_LIST_REGEX,
replace: listReplace('bullet'),
type: 'element'
};
export var ORDERED_LIST = {
dependencies: [ListNode, ListItemNode],
export: function _export(node, exportChildren) {
return $isListNode(node) ? $listExport(node, exportChildren, 0) : null;
},
regExp: ORDERED_LIST_REGEX,
replace: listReplace('number'),
type: 'element'
};
export var CHECK_LIST = {
dependencies: [ListNode, ListItemNode],
export: function _export(node, exportChildren) {
return $isListNode(node) ? $listExport(node, exportChildren, 0) : null;
},
regExp: CHECK_LIST_REGEX,
replace: listReplace('check'),
type: 'element'
};