km-card-layout-component-miniprogram 0.1.25 → 0.1.26

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.
@@ -608,4 +608,14 @@ Page({
608
608
  })
609
609
  }
610
610
  },
611
+
612
+ async onShareClick() {
613
+ const component = this.selectComponent("#card-layout");
614
+ if (component) {
615
+ const shareFilePath = await component.handleDrawShareCanvas();
616
+ this.setData({
617
+ shareFilePath,
618
+ });
619
+ }
620
+ },
611
621
  });
@@ -4,5 +4,7 @@
4
4
  <view class="subtitle">CardLayoutInput + { user } data</view>
5
5
  </view>
6
6
  <card-layout id="card-layout" layout="{{layouts}}" data="{{cardData}}" bind:tap="onClick" />
7
+ <button type="primary" bindtap="onShareClick">Draw Share Canvas</button>
7
8
  <image style="" src="{{filePath}}" mode="aspectFit" />
9
+ <image style="" src="{{shareFilePath}}" mode="aspectFit" />
8
10
  </view>
@@ -109,6 +109,7 @@ Component({
109
109
  cards: [],
110
110
  rootData: {},
111
111
  firstCard: [],
112
+ shareImage: "",
112
113
  },
113
114
  observers: {
114
115
  layout() {
@@ -150,6 +151,24 @@ Component({
150
151
  wx.showToast({ title: "保存失败", icon: "error" });
151
152
  }
152
153
  },
154
+ async handleDrawShareCanvas() {
155
+ var _a, _b, _c;
156
+ const layoutPath = await this.handleDrawCanvas();
157
+ if (!layoutPath)
158
+ return;
159
+ const shareCanvas = this.selectComponent(`#share-canvas`);
160
+ if (!shareCanvas || typeof shareCanvas.drawShare !== "function")
161
+ return;
162
+ const shareStyleId = (_c = (_b = (_a = this.data.data) === null || _a === void 0 ? void 0 : _a.user) === null || _b === void 0 ? void 0 : _b.shareStyle) === null || _c === void 0 ? void 0 : _c.coverStyleId;
163
+ const filePath = await shareCanvas.drawShare({
164
+ layoutPath,
165
+ shareStyleId,
166
+ });
167
+ if (filePath) {
168
+ this.setData({ shareImage: filePath });
169
+ }
170
+ return filePath;
171
+ },
153
172
  async loadFont() {
154
173
  await wx.loadFontFace({
155
174
  global: true,
@@ -6,6 +6,7 @@
6
6
  "icon-element": "./elements/icon-element/index",
7
7
  "custom-element": "./elements/custom-element/index",
8
8
  "layout-panel-element": "./elements/layout-panel-element/index",
9
- "wxml2canvas": "../../vendor/wxml2canvas-2d/index"
9
+ "wxml2canvas": "../../vendor/wxml2canvas-2d/index",
10
+ "share-canvas": "./share-canvas/index"
10
11
  }
11
12
  }
@@ -15,6 +15,7 @@
15
15
  <template is="layout-template" data="{{ renderCards: cards, rootData: rootData }}"></template>
16
16
  <!-- 暂时只绘制第一张卡片 -->
17
17
  <wxml2canvas id="layout-canvas" container-class="layout-container" item-class="canvas-item"></wxml2canvas>
18
+ <share-canvas id="share-canvas"></share-canvas>
18
19
  <template name="render-element">
19
20
  <block wx:if="{{el.type === 'image'}}">
20
21
  <image-element id="node-{{el.id}}" class="canvas-item" data-component="{{true}}" element="{{el}}" rootData="{{rootData}}" />
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const share_style_1 = require("./share-style");
4
+ const resolveShareStyle = (shareStyleId) => {
5
+ const styleId = Number(shareStyleId);
6
+ if (Number.isFinite(styleId) && styleId > 0) {
7
+ const match = share_style_1.ShareStyle.find((item) => item.id === styleId);
8
+ if (match)
9
+ return match;
10
+ }
11
+ return share_style_1.ShareStyle[0];
12
+ };
13
+ const resolveShareBackground = (style) => (style === null || style === void 0 ? void 0 : style.bigImg) || (style === null || style === void 0 ? void 0 : style.path) || (style === null || style === void 0 ? void 0 : style.smallImg) || "";
14
+ Component({
15
+ data: {
16
+ shareBgImage: "",
17
+ shareImage: "",
18
+ shareOffsetX: 0,
19
+ shareOffsetY: 0,
20
+ },
21
+ methods: {
22
+ setDataSync(data) {
23
+ return new Promise((resolve) => {
24
+ this.setData(data, resolve);
25
+ });
26
+ },
27
+ async drawShare(options) {
28
+ try {
29
+ const { layoutPath, shareStyleId } = options || {};
30
+ if (!layoutPath)
31
+ return;
32
+ const resolvedStyle = resolveShareStyle(shareStyleId);
33
+ const shareBgImage = resolveShareBackground(resolvedStyle);
34
+ if (!shareBgImage)
35
+ return;
36
+ const shareOffsetX = (resolvedStyle === null || resolvedStyle === void 0 ? void 0 : resolvedStyle.x) ? resolvedStyle.x * 2 : 0;
37
+ const shareOffsetY = (resolvedStyle === null || resolvedStyle === void 0 ? void 0 : resolvedStyle.y) ? resolvedStyle.y * 2 : 0;
38
+ await this.setDataSync({
39
+ shareBgImage,
40
+ shareOffsetX,
41
+ shareOffsetY,
42
+ shareImage: layoutPath,
43
+ });
44
+ const canvas = this.selectComponent(`#share-canvas`);
45
+ if (!canvas)
46
+ return;
47
+ await canvas.draw(this);
48
+ const filePath = await canvas.toTempFilePath();
49
+ return filePath;
50
+ }
51
+ catch (_a) {
52
+ wx.showToast({ title: "保存失败", icon: "error" });
53
+ }
54
+ },
55
+ },
56
+ });
@@ -0,0 +1,6 @@
1
+ {
2
+ "component": true,
3
+ "usingComponents": {
4
+ "wxml2canvas": "../../../vendor/wxml2canvas-2d/index"
5
+ }
6
+ }
@@ -0,0 +1,11 @@
1
+
2
+ <view class="share-container share-hidden">
3
+ <image class="share-bg share-item" src="{{shareBgImage}}" />
4
+ <image
5
+ class="share-card share-item"
6
+ src="{{shareImage}}"
7
+ mode="scaleToFill"
8
+ style="margin-left: {{shareOffsetX}}rpx; margin-top: {{shareOffsetY}}rpx;"
9
+ />
10
+ </view>
11
+ <wxml2canvas id="share-canvas" container-class="share-container" item-class="share-item"></wxml2canvas>
@@ -0,0 +1,27 @@
1
+ .share-container {
2
+ position: relative;
3
+ box-sizing: border-box;
4
+ width: 422rpx;
5
+ height: 338rpx;
6
+ }
7
+
8
+ .share-hidden {
9
+ position: fixed;
10
+ left: -10000rpx;
11
+ top: -10000rpx;
12
+ opacity: 0;
13
+ }
14
+
15
+ .share-bg {
16
+ position: absolute;
17
+ left: 0;
18
+ top: 0;
19
+ width: 422rpx;
20
+ height: 338rpx;
21
+ }
22
+
23
+ .share-card {
24
+ width: 402rpx;
25
+ height: 247rpx;
26
+ border-radius: 16rpx;
27
+ }
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ShareStyle = void 0;
4
+ exports.ShareStyle = [
5
+ {
6
+ id: 1,
7
+ bigImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_1.png",
8
+ smallImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_1.png",
9
+ path: "images/cardstyleV3/background_cover_1.png",
10
+ x: 6,
11
+ y: 12,
12
+ },
13
+ {
14
+ id: 2,
15
+ bigImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_2.png",
16
+ smallImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_2.png",
17
+ path: "images/cardstyleV3/background_cover_2.png",
18
+ x: 6,
19
+ y: 35,
20
+ },
21
+ {
22
+ id: 3,
23
+ bigImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_3.png",
24
+ smallImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_3.png",
25
+ path: "images/cardstyleV3/background_cover_3.png",
26
+ x: 6,
27
+ y: 12,
28
+ },
29
+ {
30
+ id: 4,
31
+ bigImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_4.png",
32
+ smallImg: "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_4.png",
33
+ path: "images/cardstyleV3/background_cover_4.png",
34
+ x: 6,
35
+ y: 24,
36
+ },
37
+ ];
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ShareStyle = void 0;
4
+ var share_style_1 = require("./share-canvas/share-style");
5
+ Object.defineProperty(exports, "ShareStyle", { enumerable: true, get: function () { return share_style_1.ShareStyle; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "km-card-layout-component-miniprogram",
3
- "version": "0.1.25",
3
+ "version": "0.1.26",
4
4
  "description": "",
5
5
  "main": "miniprogram_dist/index.js",
6
6
  "miniprogram": "miniprogram_dist",
@@ -6,6 +6,7 @@
6
6
  "icon-element": "./elements/icon-element/index",
7
7
  "custom-element": "./elements/custom-element/index",
8
8
  "layout-panel-element": "./elements/layout-panel-element/index",
9
- "wxml2canvas": "../../vendor/wxml2canvas-2d/index"
9
+ "wxml2canvas": "../../vendor/wxml2canvas-2d/index",
10
+ "share-canvas": "./share-canvas/index"
10
11
  }
11
12
  }
@@ -133,7 +133,7 @@ Component({
133
133
  data: {
134
134
  type: Object,
135
135
  value: {
136
- user: {},
136
+ user: {} as AnyObject,
137
137
  },
138
138
  },
139
139
  },
@@ -141,6 +141,7 @@ Component({
141
141
  cards: [] as RenderCard[],
142
142
  rootData: {} as Record<string, any>,
143
143
  firstCard: [] as RenderCard[],
144
+ shareImage: "",
144
145
  },
145
146
  observers: {
146
147
  layout() {
@@ -183,6 +184,24 @@ Component({
183
184
  }
184
185
  },
185
186
 
187
+ async handleDrawShareCanvas() {
188
+ const layoutPath = await this.handleDrawCanvas();
189
+ if (!layoutPath) return;
190
+
191
+ const shareCanvas = this.selectComponent(`#share-canvas`) as any;
192
+ if (!shareCanvas || typeof shareCanvas.drawShare !== "function") return;
193
+
194
+ const shareStyleId = this.data.data?.user?.shareStyle?.coverStyleId ;
195
+ const filePath = await shareCanvas.drawShare({
196
+ layoutPath,
197
+ shareStyleId,
198
+ });
199
+ if (filePath) {
200
+ this.setData({ shareImage: filePath });
201
+ }
202
+ return filePath;
203
+ },
204
+
186
205
  async loadFont() {
187
206
  await wx.loadFontFace({
188
207
  global: true,
@@ -15,6 +15,7 @@
15
15
  <template is="layout-template" data="{{ renderCards: cards, rootData: rootData }}"></template>
16
16
  <!-- 暂时只绘制第一张卡片 -->
17
17
  <wxml2canvas id="layout-canvas" container-class="layout-container" item-class="canvas-item"></wxml2canvas>
18
+ <share-canvas id="share-canvas"></share-canvas>
18
19
  <template name="render-element">
19
20
  <block wx:if="{{el.type === 'image'}}">
20
21
  <image-element id="node-{{el.id}}" class="canvas-item" data-component="{{true}}" element="{{el}}" rootData="{{rootData}}" />
@@ -0,0 +1,6 @@
1
+ {
2
+ "component": true,
3
+ "usingComponents": {
4
+ "wxml2canvas": "../../../vendor/wxml2canvas-2d/index"
5
+ }
6
+ }
@@ -0,0 +1,59 @@
1
+ import { ShareStyle } from "./share-style";
2
+
3
+ type ShareStyleItem = (typeof ShareStyle)[number];
4
+
5
+ const resolveShareStyle = (shareStyleId?: number | string): ShareStyleItem => {
6
+ const styleId = Number(shareStyleId);
7
+ if (Number.isFinite(styleId) && styleId > 0) {
8
+ const match = ShareStyle.find((item) => item.id === styleId);
9
+ if (match) return match;
10
+ }
11
+ return ShareStyle[0];
12
+ };
13
+
14
+ const resolveShareBackground = (style?: ShareStyleItem) =>
15
+ style?.bigImg || style?.path || style?.smallImg || "";
16
+
17
+ Component({
18
+ data: {
19
+ shareBgImage: "",
20
+ shareImage: "",
21
+ shareOffsetX: 0,
22
+ shareOffsetY: 0,
23
+ },
24
+ methods: {
25
+ setDataSync(data: Record<string, any>) {
26
+ return new Promise<void>((resolve) => {
27
+ this.setData(data, resolve);
28
+ });
29
+ },
30
+
31
+ async drawShare(options: { layoutPath: string; shareStyleId?: number | string }) {
32
+ try {
33
+ const { layoutPath, shareStyleId } = options || {};
34
+ if (!layoutPath) return;
35
+
36
+ const resolvedStyle = resolveShareStyle(shareStyleId);
37
+ const shareBgImage = resolveShareBackground(resolvedStyle);
38
+ if (!shareBgImage) return;
39
+
40
+ const shareOffsetX = resolvedStyle?.x ? resolvedStyle.x * 2 : 0;
41
+ const shareOffsetY = resolvedStyle?.y ? resolvedStyle.y * 2 : 0;
42
+ await this.setDataSync({
43
+ shareBgImage,
44
+ shareOffsetX,
45
+ shareOffsetY,
46
+ shareImage: layoutPath,
47
+ });
48
+
49
+ const canvas = this.selectComponent(`#share-canvas`);
50
+ if (!canvas) return;
51
+ await canvas.draw(this);
52
+ const filePath = await canvas.toTempFilePath();
53
+ return filePath;
54
+ } catch {
55
+ wx.showToast({ title: "保存失败", icon: "error" });
56
+ }
57
+ },
58
+ },
59
+ });
@@ -0,0 +1,11 @@
1
+
2
+ <view class="share-container share-hidden">
3
+ <image class="share-bg share-item" src="{{shareBgImage}}" />
4
+ <image
5
+ class="share-card share-item"
6
+ src="{{shareImage}}"
7
+ mode="scaleToFill"
8
+ style="margin-left: {{shareOffsetX}}rpx; margin-top: {{shareOffsetY}}rpx;"
9
+ />
10
+ </view>
11
+ <wxml2canvas id="share-canvas" container-class="share-container" item-class="share-item"></wxml2canvas>
@@ -0,0 +1,27 @@
1
+ .share-container {
2
+ position: relative;
3
+ box-sizing: border-box;
4
+ width: 422rpx;
5
+ height: 338rpx;
6
+ }
7
+
8
+ .share-hidden {
9
+ position: fixed;
10
+ left: -10000rpx;
11
+ top: -10000rpx;
12
+ opacity: 0;
13
+ }
14
+
15
+ .share-bg {
16
+ position: absolute;
17
+ left: 0;
18
+ top: 0;
19
+ width: 422rpx;
20
+ height: 338rpx;
21
+ }
22
+
23
+ .share-card {
24
+ width: 402rpx;
25
+ height: 247rpx;
26
+ border-radius: 16rpx;
27
+ }
@@ -0,0 +1,42 @@
1
+ export const ShareStyle = [
2
+ {
3
+ id: 1,
4
+ bigImg:
5
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_1.png",
6
+ smallImg:
7
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_1.png",
8
+ path: "images/cardstyleV3/background_cover_1.png",
9
+ x: 6,
10
+ y: 12,
11
+ },
12
+ {
13
+ id: 2,
14
+ bigImg:
15
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_2.png",
16
+ smallImg:
17
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_2.png",
18
+ path: "images/cardstyleV3/background_cover_2.png",
19
+ x: 6,
20
+ y: 35,
21
+ },
22
+ {
23
+ id: 3,
24
+ bigImg:
25
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_3.png",
26
+ smallImg:
27
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_3.png",
28
+ path: "images/cardstyleV3/background_cover_3.png",
29
+ x: 6,
30
+ y: 12,
31
+ },
32
+ {
33
+ id: 4,
34
+ bigImg:
35
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_4.png",
36
+ smallImg:
37
+ "https://km-1257079185.cos.ap-chengdu.myqcloud.com/static/cardstyleV3/background_cover_small_4.png",
38
+ path: "images/cardstyleV3/background_cover_4.png",
39
+ x: 6,
40
+ y: 24,
41
+ },
42
+ ];
@@ -0,0 +1 @@
1
+ export { ShareStyle } from "./share-canvas/share-style";