km-card-layout-component-miniprogram 0.1.15 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizeMoreCompany = void 0;
3
+ exports.hasCompanyDutyKey = exports.normalizeMoreCompany = void 0;
4
4
  const index_1 = require("../../vendor/km-card-layout-core/index");
5
5
  const EMPTY_COMPANY_DUTY = {
6
6
  company: '',
@@ -88,6 +88,29 @@ const buildCards = (layouts) => {
88
88
  elements: decorateElements(layout.children || []),
89
89
  }));
90
90
  };
91
+ const hasCompanyDutyKey = (schemas) => {
92
+ const TARGET_KEYS = new Set([
93
+ 'company_duty_custom',
94
+ 'company_duty_1',
95
+ 'company_duty_2',
96
+ ]);
97
+ const traverse = (elements) => {
98
+ return elements.some(el => {
99
+ var _a;
100
+ // 命中 key
101
+ if (el.key && TARGET_KEYS.has(el.key)) {
102
+ return true;
103
+ }
104
+ // 递归 layout-panel
105
+ if (el.type === 'layout-panel' && ((_a = el.children) === null || _a === void 0 ? void 0 : _a.length)) {
106
+ return traverse(el.children);
107
+ }
108
+ return false;
109
+ });
110
+ };
111
+ return schemas.some(schema => traverse(schema.children));
112
+ };
113
+ exports.hasCompanyDutyKey = hasCompanyDutyKey;
91
114
  Component({
92
115
  options: {
93
116
  styleIsolation: 'apply-shared',
@@ -125,7 +148,7 @@ Component({
125
148
  methods: {
126
149
  rebuild() {
127
150
  const data = normalizeMoreCompany(this.data.data);
128
- const layoutInput = (0, index_1.processCardLayout)(this.data.layout, data);
151
+ const layoutInput = (0, exports.hasCompanyDutyKey)(this.data.layout) ? (0, index_1.processCardLayout)(this.data.layout, data) : this.data.layout;
129
152
  const rootData = handleSpecialFields(data);
130
153
  if (!layoutInput.length) {
131
154
  this.setData({ cards: [], rootData });
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.processCardLayout = exports.handleSpecialFields = exports.buildTextIconMeta = exports.getIconName = exports.getImageSrc = exports.getTextValue = exports.buildImageContentStyle = exports.buildTextValueStyle = exports.buildTextContentStyle = exports.buildPanelContentStyle = exports.buildBaseContentStyle = exports.buildBackgroundStyle = exports.buildCardStyle = exports.buildWrapperStyle = exports.backgroundChange = exports.applyItemCollectBindings = exports.stripLayoutBindings = exports.resolveBindingValue = exports.parseLayout = exports.normalizeId = exports.getAbsLayout = exports.roundToInt = exports.sanitizeElement = exports.sanitizeLayout = exports.normalizeLayout = exports.isObject = exports.toNumber = exports.styleObjectToString = exports.addUnit = void 0;
17
+ exports.processCardLayout = exports.handleSpecialFields = exports.setElementVisible = exports.buildRenderData = exports.buildTextIconMeta = exports.getIconName = exports.getImageSrc = exports.getTextValue = exports.buildImageContentStyle = exports.buildTextValueStyle = exports.buildTextContentStyle = exports.buildPanelContentStyle = exports.buildBaseContentStyle = exports.buildBackgroundStyle = exports.buildCardStyle = exports.buildWrapperStyle = exports.backgroundChange = exports.applyItemCollectBindings = exports.stripLayoutBindings = exports.resolveBindingValue = exports.parseLayout = exports.normalizeId = exports.getAbsLayout = exports.roundToInt = exports.sanitizeElement = exports.sanitizeLayout = exports.normalizeLayout = exports.isObject = exports.toNumber = exports.styleObjectToString = exports.addUnit = void 0;
18
18
  /**
19
19
  * Core exports for card layout helpers.
20
20
  * Platform-agnostic utilities shared by web/mini-program renderers.
@@ -53,6 +53,9 @@ Object.defineProperty(exports, "getTextValue", { enumerable: true, get: function
53
53
  Object.defineProperty(exports, "getImageSrc", { enumerable: true, get: function () { return helpers_2.getImageSrc; } });
54
54
  Object.defineProperty(exports, "getIconName", { enumerable: true, get: function () { return helpers_2.getIconName; } });
55
55
  Object.defineProperty(exports, "buildTextIconMeta", { enumerable: true, get: function () { return helpers_2.buildTextIconMeta; } });
56
+ var tool_1 = require("./render/tool");
57
+ Object.defineProperty(exports, "buildRenderData", { enumerable: true, get: function () { return tool_1.buildRenderData; } });
58
+ Object.defineProperty(exports, "setElementVisible", { enumerable: true, get: function () { return tool_1.setElementVisible; } });
56
59
  var init_1 = require("./render/init");
57
60
  Object.defineProperty(exports, "handleSpecialFields", { enumerable: true, get: function () { return init_1.handleSpecialFields; } });
58
61
  Object.defineProperty(exports, "processCardLayout", { enumerable: true, get: function () { return init_1.processCardLayout; } });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildRenderData = exports.setElementVisible = void 0;
4
+ const bindings_1 = require("../bindings");
5
+ const changeBackground_1 = require("../ops/changeBackground");
6
+ const setElementVisible = (layout, key, visible) => {
7
+ return layout.map(la => ({
8
+ ...la,
9
+ children: la.children.map(el => (el.key === key ? { ...el, visible } : el)),
10
+ }));
11
+ };
12
+ exports.setElementVisible = setElementVisible;
13
+ function buildRenderData(layout, items, config) {
14
+ // 绑定数据
15
+ let renderLayout = (0, bindings_1.applyItemCollectBindings)(layout, items);
16
+ // 设置显示逻辑
17
+ renderLayout = (0, exports.setElementVisible)(renderLayout, 'image', config.showAvatar);
18
+ renderLayout = (0, exports.setElementVisible)(renderLayout, 'logo', config.showLogo);
19
+ // 设置背景
20
+ renderLayout = renderLayout.map((la, index) => config.currentBg[index] ? (0, changeBackground_1.backgroundChange)(config.currentBg[index], la) : la);
21
+ return renderLayout;
22
+ }
23
+ exports.buildRenderData = buildRenderData;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "km-card-layout-component-miniprogram",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "",
5
5
  "main": "miniprogram_dist/index.js",
6
6
  "miniprogram": "miniprogram_dist",
@@ -135,6 +135,36 @@ const buildCards = (layouts: CardLayoutSchema[]) => {
135
135
  }));
136
136
  };
137
137
 
138
+
139
+ export const hasCompanyDutyKey = (
140
+ schemas: CardLayoutSchema[]
141
+ ): boolean => {
142
+ const TARGET_KEYS = new Set([
143
+ 'company_duty_custom',
144
+ 'company_duty_1',
145
+ 'company_duty_2',
146
+ ]);
147
+
148
+ const traverse = (elements: CardElement[]): boolean => {
149
+ return elements.some(el => {
150
+ // 命中 key
151
+ if (el.key && TARGET_KEYS.has(el.key)) {
152
+ return true;
153
+ }
154
+
155
+ // 递归 layout-panel
156
+ if (el.type === 'layout-panel' && el.children?.length) {
157
+ return traverse(el.children);
158
+ }
159
+
160
+ return false;
161
+ });
162
+ };
163
+
164
+ return schemas.some(schema => traverse(schema.children));
165
+ };
166
+
167
+
138
168
  Component({
139
169
  options: {
140
170
  styleIsolation: 'apply-shared',
@@ -172,7 +202,7 @@ Component({
172
202
  methods: {
173
203
  rebuild() {
174
204
  const data = normalizeMoreCompany(this.data.data)
175
- const layoutInput = processCardLayout(this.data.layout, data as any);
205
+ const layoutInput = hasCompanyDutyKey(this.data.layout)?processCardLayout(this.data.layout, data as any): this.data.layout;
176
206
  const rootData = handleSpecialFields(data as any);
177
207
  if (!layoutInput.length) {
178
208
  this.setData({ cards: [], rootData });
@@ -30,5 +30,6 @@ export {
30
30
  getIconName,
31
31
  buildTextIconMeta,
32
32
  } from './render/helpers';
33
+ export { buildRenderData, setElementVisible } from './render/tool';
33
34
 
34
35
  export { handleSpecialFields, processCardLayout } from './render/init';
@@ -0,0 +1,34 @@
1
+ import { applyItemCollectBindings } from '../bindings';
2
+ import { CardLayoutSchema, TemplateItem } from '../interface';
3
+ import { backgroundChange } from '../ops/changeBackground';
4
+ import { Config } from '../types';
5
+
6
+
7
+ export const setElementVisible = (
8
+ layout: CardLayoutSchema[],
9
+ key: string,
10
+ visible: boolean
11
+ ): CardLayoutSchema[] => {
12
+ return layout.map(la => ({
13
+ ...la,
14
+ children: la.children.map(el => (el.key === key ? { ...el, visible } : el)),
15
+ }));
16
+ };
17
+
18
+
19
+ export function buildRenderData(
20
+ layout: CardLayoutSchema[],
21
+ items: TemplateItem[],
22
+ config: Config
23
+ ): CardLayoutSchema[] {
24
+ // 绑定数据
25
+ let renderLayout = applyItemCollectBindings(layout, items);
26
+ // 设置显示逻辑
27
+ renderLayout = setElementVisible(renderLayout, 'image', config.showAvatar);
28
+ renderLayout = setElementVisible(renderLayout, 'logo', config.showLogo);
29
+ // 设置背景
30
+ renderLayout = renderLayout.map((la, index) =>
31
+ config.currentBg[index] ? backgroundChange(config.currentBg[index], la) : la
32
+ );
33
+ return renderLayout;
34
+ }
@@ -144,4 +144,13 @@ export function handleSpecialFields(data: { user: Record<string, any> }): {
144
144
  export function processCardLayout(
145
145
  layout: CardLayoutSchema[],
146
146
  data: Data
147
- ): CardLayoutSchema[];
147
+ ): CardLayoutSchema[];
148
+
149
+ export type Config = {
150
+ showAvatar: boolean;
151
+ showLogo: boolean;
152
+ /**
153
+ * 当前背景 (0 为正面背景 1 为背面背景)
154
+ */
155
+ currentBg: TemplateBackground[];
156
+ };