index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import _extends from "@babel/runtime/helpers/esm/extends";
  2. import classNames from '../classNames';
  3. import { isVNode, Fragment, Comment, Text } from 'vue';
  4. import { camelize, hyphenate, isOn, resolvePropValue } from '../util';
  5. import isValid from '../isValid';
  6. import initDefaultProps from './initDefaultProps';
  7. // function getType(fn) {
  8. // const match = fn && fn.toString().match(/^\s*function (\w+)/);
  9. // return match ? match[1] : '';
  10. // }
  11. const splitAttrs = attrs => {
  12. const allAttrs = Object.keys(attrs);
  13. const eventAttrs = {};
  14. const onEvents = {};
  15. const extraAttrs = {};
  16. for (let i = 0, l = allAttrs.length; i < l; i++) {
  17. const key = allAttrs[i];
  18. if (isOn(key)) {
  19. eventAttrs[key[2].toLowerCase() + key.slice(3)] = attrs[key];
  20. onEvents[key] = attrs[key];
  21. } else {
  22. extraAttrs[key] = attrs[key];
  23. }
  24. }
  25. return {
  26. onEvents,
  27. events: eventAttrs,
  28. extraAttrs
  29. };
  30. };
  31. const parseStyleText = function () {
  32. let cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
  33. let camel = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  34. const res = {};
  35. const listDelimiter = /;(?![^(]*\))/g;
  36. const propertyDelimiter = /:(.+)/;
  37. if (typeof cssText === 'object') return cssText;
  38. cssText.split(listDelimiter).forEach(function (item) {
  39. if (item) {
  40. const tmp = item.split(propertyDelimiter);
  41. if (tmp.length > 1) {
  42. const k = camel ? camelize(tmp[0].trim()) : tmp[0].trim();
  43. res[k] = tmp[1].trim();
  44. }
  45. }
  46. });
  47. return res;
  48. };
  49. const hasProp = (instance, prop) => {
  50. return instance[prop] !== undefined;
  51. };
  52. export const skipFlattenKey = Symbol('skipFlatten');
  53. const flattenChildren = function () {
  54. let children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  55. let filterEmpty = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  56. const temp = Array.isArray(children) ? children : [children];
  57. const res = [];
  58. temp.forEach(child => {
  59. if (Array.isArray(child)) {
  60. res.push(...flattenChildren(child, filterEmpty));
  61. } else if (child && child.type === Fragment) {
  62. if (child.key === skipFlattenKey) {
  63. res.push(child);
  64. } else {
  65. res.push(...flattenChildren(child.children, filterEmpty));
  66. }
  67. } else if (child && isVNode(child)) {
  68. if (filterEmpty && !isEmptyElement(child)) {
  69. res.push(child);
  70. } else if (!filterEmpty) {
  71. res.push(child);
  72. }
  73. } else if (isValid(child)) {
  74. res.push(child);
  75. }
  76. });
  77. return res;
  78. };
  79. const getSlot = function (self) {
  80. let name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  81. let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  82. if (isVNode(self)) {
  83. if (self.type === Fragment) {
  84. return name === 'default' ? flattenChildren(self.children) : [];
  85. } else if (self.children && self.children[name]) {
  86. return flattenChildren(self.children[name](options));
  87. } else {
  88. return [];
  89. }
  90. } else {
  91. const res = self.$slots[name] && self.$slots[name](options);
  92. return flattenChildren(res);
  93. }
  94. };
  95. const findDOMNode = instance => {
  96. var _a;
  97. let node = ((_a = instance === null || instance === void 0 ? void 0 : instance.vnode) === null || _a === void 0 ? void 0 : _a.el) || instance && (instance.$el || instance);
  98. while (node && !node.tagName) {
  99. node = node.nextSibling;
  100. }
  101. return node;
  102. };
  103. const getOptionProps = instance => {
  104. const res = {};
  105. if (instance.$ && instance.$.vnode) {
  106. const props = instance.$.vnode.props || {};
  107. Object.keys(instance.$props).forEach(k => {
  108. const v = instance.$props[k];
  109. const hyphenateKey = hyphenate(k);
  110. if (v !== undefined || hyphenateKey in props) {
  111. res[k] = v; // 直接取 $props[k]
  112. }
  113. });
  114. } else if (isVNode(instance) && typeof instance.type === 'object') {
  115. const originProps = instance.props || {};
  116. const props = {};
  117. Object.keys(originProps).forEach(key => {
  118. props[camelize(key)] = originProps[key];
  119. });
  120. const options = instance.type.props || {};
  121. Object.keys(options).forEach(k => {
  122. const v = resolvePropValue(options, props, k, props[k]);
  123. if (v !== undefined || k in props) {
  124. res[k] = v;
  125. }
  126. });
  127. }
  128. return res;
  129. };
  130. const getComponent = function (instance) {
  131. let prop = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'default';
  132. let options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : instance;
  133. let execute = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
  134. let com = undefined;
  135. if (instance.$) {
  136. const temp = instance[prop];
  137. if (temp !== undefined) {
  138. return typeof temp === 'function' && execute ? temp(options) : temp;
  139. } else {
  140. com = instance.$slots[prop];
  141. com = execute && com ? com(options) : com;
  142. }
  143. } else if (isVNode(instance)) {
  144. const temp = instance.props && instance.props[prop];
  145. if (temp !== undefined && instance.props !== null) {
  146. return typeof temp === 'function' && execute ? temp(options) : temp;
  147. } else if (instance.type === Fragment) {
  148. com = instance.children;
  149. } else if (instance.children && instance.children[prop]) {
  150. com = instance.children[prop];
  151. com = execute && com ? com(options) : com;
  152. }
  153. }
  154. if (Array.isArray(com)) {
  155. com = flattenChildren(com);
  156. com = com.length === 1 ? com[0] : com;
  157. com = com.length === 0 ? undefined : com;
  158. }
  159. return com;
  160. };
  161. const getKey = ele => {
  162. const key = ele.key;
  163. return key;
  164. };
  165. export function getEvents() {
  166. let ele = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  167. let on = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  168. let props = {};
  169. if (ele.$) {
  170. props = _extends(_extends({}, props), ele.$attrs);
  171. } else {
  172. props = _extends(_extends({}, props), ele.props);
  173. }
  174. return splitAttrs(props)[on ? 'onEvents' : 'events'];
  175. }
  176. export function getClass(ele) {
  177. const props = (isVNode(ele) ? ele.props : ele.$attrs) || {};
  178. const tempCls = props.class || {};
  179. let cls = {};
  180. if (typeof tempCls === 'string') {
  181. tempCls.split(' ').forEach(c => {
  182. cls[c.trim()] = true;
  183. });
  184. } else if (Array.isArray(tempCls)) {
  185. classNames(tempCls).split(' ').forEach(c => {
  186. cls[c.trim()] = true;
  187. });
  188. } else {
  189. cls = _extends(_extends({}, cls), tempCls);
  190. }
  191. return cls;
  192. }
  193. export function getStyle(ele, camel) {
  194. const props = (isVNode(ele) ? ele.props : ele.$attrs) || {};
  195. let style = props.style || {};
  196. if (typeof style === 'string') {
  197. style = parseStyleText(style, camel);
  198. } else if (camel && style) {
  199. // 驼峰化
  200. const res = {};
  201. Object.keys(style).forEach(k => res[camelize(k)] = style[k]);
  202. return res;
  203. }
  204. return style;
  205. }
  206. export function getComponentName(opts) {
  207. return opts && (opts.Ctor.options.name || opts.tag);
  208. }
  209. export function isFragment(c) {
  210. return c.length === 1 && c[0].type === Fragment;
  211. }
  212. export function isEmptyContent(c) {
  213. return c === undefined || c === null || c === '' || Array.isArray(c) && c.length === 0;
  214. }
  215. export function isEmptyElement(c) {
  216. return c && (c.type === Comment || c.type === Fragment && c.children.length === 0 || c.type === Text && c.children.trim() === '');
  217. }
  218. export function isEmptySlot(c) {
  219. return !c || c().every(isEmptyElement);
  220. }
  221. export function isStringElement(c) {
  222. return c && c.type === Text;
  223. }
  224. export function filterEmpty() {
  225. let children = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  226. const res = [];
  227. children.forEach(child => {
  228. if (Array.isArray(child)) {
  229. res.push(...child);
  230. } else if ((child === null || child === void 0 ? void 0 : child.type) === Fragment) {
  231. res.push(...filterEmpty(child.children));
  232. } else {
  233. res.push(child);
  234. }
  235. });
  236. return res.filter(c => !isEmptyElement(c));
  237. }
  238. export function filterEmptyWithUndefined(children) {
  239. if (children) {
  240. const coms = filterEmpty(children);
  241. return coms.length ? coms : undefined;
  242. } else {
  243. return children;
  244. }
  245. }
  246. function isValidElement(element) {
  247. if (Array.isArray(element) && element.length === 1) {
  248. element = element[0];
  249. }
  250. return element && element.__v_isVNode && typeof element.type !== 'symbol'; // remove text node
  251. }
  252. function getPropsSlot(slots, props) {
  253. let prop = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'default';
  254. var _a, _b;
  255. return (_a = props[prop]) !== null && _a !== void 0 ? _a : (_b = slots[prop]) === null || _b === void 0 ? void 0 : _b.call(slots);
  256. }
  257. export const getTextFromElement = ele => {
  258. if (isValidElement(ele) && isStringElement(ele[0])) {
  259. return ele[0].children;
  260. }
  261. return ele;
  262. };
  263. export { splitAttrs, hasProp, getOptionProps, getComponent, getKey, parseStyleText, initDefaultProps, isValidElement, camelize, getSlot, findDOMNode, flattenChildren, getPropsSlot };
  264. export default hasProp;