| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import { __read, __spreadArray } from "tslib";
- import { addPrefix, removePrefix, toUppercaseFirstLetter } from './string';
- /**
- * 对给定HTML对象应用给定样式
- * @param style {[key: string]: Object}
- * 样式表参考结构
- * {
- * '.selector': {
- * 'attrName': 'attr',
- * 'padding': '0 0 0 0',
- * 'background-color': 'red'
- * }
- * }
- */
- export function applyStyleSheet(element, style) {
- Object.entries(style).forEach(function (_a) {
- var _b = __read(_a, 2), selector = _b[0], styleString = _b[1];
- // apply styles to element and children
- __spreadArray([element], __read(element.querySelectorAll(selector)), false).filter(function (el) { return el.matches(selector); })
- .forEach(function (target) {
- if (!target)
- return;
- var temp = target;
- temp.style.cssText += Object.entries(styleString).reduce(function (total, currVal) {
- return "".concat(total).concat(currVal.join(':'), ";");
- }, '');
- });
- });
- }
- export function subStyleProps(style, prefix, invert) {
- if (invert === void 0) { invert = false; }
- var result = {};
- Object.entries(style).forEach(function (_a) {
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
- // never transfer class property
- if (key === 'className' || key === 'class') {
- // do nothing
- }
- // @example showHandle -> showHandle, showHandleLabel -> showLabel
- else if (key.startsWith('show') && removePrefix(key, 'show').startsWith(prefix) !== invert) {
- if (key === addPrefix(prefix, 'show'))
- result[key] = value;
- else
- result[key.replace(new RegExp(toUppercaseFirstLetter(prefix)), '')] = value;
- }
- // @example navFormatter -> formatter
- else if (!key.startsWith('show') && key.startsWith(prefix) !== invert) {
- var name_1 = removePrefix(key, prefix);
- // don't transfer filter if it represents “过滤器”
- if (name_1 === 'filter' && typeof value === 'function') {
- // do nothing
- }
- else
- result[name_1] = value;
- }
- });
- return result;
- }
- export function superStyleProps(style, prefix) {
- return Object.entries(style).reduce(function (acc, _a) {
- var _b = __read(_a, 2), key = _b[0], value = _b[1];
- if (key.startsWith('show'))
- acc["show".concat(prefix).concat(key.slice(4))] = value;
- else
- acc["".concat(prefix).concat(toUppercaseFirstLetter(key))] = value;
- return acc;
- }, {});
- }
- /**
- * extract group style from mixin style
- * @param style
- * @param ignoreStyleDict style will be ignore from style
- * @returns shape style and rest style
- */
- export function splitStyle(style, ignoreStyleDict) {
- if (ignoreStyleDict === void 0) { ignoreStyleDict = ['x', 'y', 'class', 'className']; }
- var groupStyleDict = [
- 'transform',
- 'transformOrigin',
- 'anchor',
- 'visibility',
- 'pointerEvents',
- 'zIndex',
- 'cursor',
- 'clipPath',
- 'clipPathTargets',
- 'offsetPath',
- 'offsetPathTargets',
- 'offsetDistance',
- 'draggable',
- 'droppable',
- ];
- var output = {};
- var groupStyle = {};
- Object.entries(style).forEach(function (_a) {
- var _b = __read(_a, 2), key = _b[0], val = _b[1];
- if (ignoreStyleDict.includes(key)) {
- // do nothing
- }
- else if (groupStyleDict.indexOf(key) !== -1)
- groupStyle[key] = val;
- else
- output[key] = val;
- });
- return [output, groupStyle];
- }
- //# sourceMappingURL=style.js.map
|