@egjs/vue3-flicking 4.4.1 → 4.4.2

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.
package/src/Flicking.ts CHANGED
@@ -2,10 +2,7 @@
2
2
  * Copyright (c) 2015 NAVER Corp.
3
3
  * egjs projects are licensed under the MIT license
4
4
  */
5
- /* eslint-disable @typescript-eslint/explicit-member-accessibility */
6
- /* eslint-disable @typescript-eslint/naming-convention */
7
- import { h, VNode, resolveComponent, Fragment, getCurrentInstance } from "vue";
8
- import { Vue, Options, VueConstructor } from "vue-class-component";
5
+ import { h, defineComponent, VNode, resolveComponent, Fragment, getCurrentInstance } from "vue";
9
6
  import ListDiffer, { DiffResult } from "@egjs/list-differ";
10
7
  import Component from "@egjs/component";
11
8
  import VanillaFlicking, {
@@ -17,47 +14,100 @@ import VanillaFlicking, {
17
14
  getDefaultCameraTransform,
18
15
  range,
19
16
  VirtualRenderingStrategy,
20
- NormalRenderingStrategy,
21
- ExternalPanel
17
+ NormalRenderingStrategy
22
18
  } from "@egjs/flicking";
23
19
 
24
20
  import FlickingProps from "./FlickingProps";
25
21
  import VueRenderer, { VueRendererOptions } from "./VueRenderer";
26
22
  import VuePanel from "./VuePanel";
27
23
  import VueElementProvider from "./VueElementProvider";
24
+ import { VueFlicking } from "./types";
28
25
 
29
- @Options({
26
+ const Flicking = defineComponent({
27
+ props: FlickingProps,
30
28
  components: {
31
29
  Panel: VuePanel
32
- }
33
- })
34
- class Flicking extends Vue.with(FlickingProps) {
35
- public readonly renderEmitter = new Component<{ render: void }>();
36
-
37
- @withFlickingMethods private vanillaFlicking: VanillaFlicking | null = null;
38
- private pluginsDiffer!: ListDiffer<Plugin>;
39
- private slotDiffer!: ListDiffer<VNode>;
40
- private diffResult?: DiffResult<VNode> = undefined;
30
+ },
31
+ data() {
32
+ return {} as {
33
+ renderEmitter: Component<{ render: void }>;
34
+ vanillaFlicking: VanillaFlicking;
35
+ pluginsDiffer: ListDiffer<Plugin>;
36
+ slotDiffer: ListDiffer<VNode>;
37
+ diffResult: DiffResult<VNode> | null;
38
+ };
39
+ },
40
+ created() {
41
+ this.vanillaFlicking = null;
42
+ this.renderEmitter = new Component();
43
+ this.diffResult = null;
44
+
45
+ this.getPanels = () => {
46
+ const componentInstance = getCurrentInstance() as unknown as { ctx: Flicking } | null;
47
+ const vueFlicking = componentInstance?.ctx;
48
+ const flicking = this.vanillaFlicking;
49
+ const defaultSlots = this.getSlots();
50
+ const diffResult = vueFlicking?.diffResult;
51
+
52
+ const slots = diffResult
53
+ ? getRenderingPanels(flicking, diffResult)
54
+ : defaultSlots;
55
+
56
+ const panelComponent = resolveComponent("Panel");
57
+ const panels = slots.map((slot, idx) => h(panelComponent as any, {
58
+ key: slot.key!,
59
+ ref: idx.toString()
60
+ }, () => slot));
61
+
62
+ return panels;
63
+ };
64
+ this.getVirtualPanels = () => {
65
+ const options = this.options;
66
+ const {
67
+ panelClass = "flicking-panel"
68
+ } = options.virtual!;
69
+ const panelsPerView = options.panelsPerView as number;
70
+ const flicking = this.vanillaFlicking;
71
+ const initialized = flicking && flicking.initialized;
72
+
73
+ const renderingIndexes = initialized
74
+ ? flicking.renderer.strategy.getRenderingIndexesByOrder(flicking)
75
+ : range(panelsPerView + 1);
76
+
77
+ const firstPanel = initialized && flicking.panels[0];
78
+ const size = firstPanel
79
+ ? flicking.horizontal
80
+ ? { width: firstPanel.size }
81
+ : { height: firstPanel.size }
82
+ : {};
83
+
84
+ return renderingIndexes.map(idx => h("div", {
85
+ key: idx,
86
+ ref: idx.toString(),
87
+ class: panelClass,
88
+ style: size,
89
+ "data-element-index": idx
90
+ }));
91
+ };
41
92
 
42
- public mounted() {
93
+ withFlickingMethods(this, "vanillaFlicking");
94
+ },
95
+ mounted() {
43
96
  const options = this.options;
44
97
  const viewportEl = this.$el as HTMLElement;
45
98
  const rendererOptions: VueRendererOptions = {
46
99
  vueFlicking: this,
100
+ align: options.align,
47
101
  strategy: options.virtual && (options.panelsPerView ?? -1) > 0
48
102
  ? new VirtualRenderingStrategy()
49
103
  : new NormalRenderingStrategy({
50
- providerCtor: VueElementProvider,
51
- panelCtor: ExternalPanel
104
+ providerCtor: VueElementProvider
52
105
  })
53
106
  };
54
107
 
55
108
  const flicking = new VanillaFlicking(viewportEl, {
56
- ...this.options,
57
- ...{ renderExternal: {
58
- renderer: VueRenderer,
59
- rendererOptions
60
- }}
109
+ ...options,
110
+ externalRenderer: new VueRenderer(rendererOptions)
61
111
  });
62
112
  this.vanillaFlicking = flicking;
63
113
 
@@ -75,23 +125,19 @@ class Flicking extends Vue.with(FlickingProps) {
75
125
  if (this.status) {
76
126
  flicking.setStatus(this.status);
77
127
  }
78
- }
79
-
80
- public beforeDestroy() {
128
+ },
129
+ beforeUnmount() {
81
130
  this.vanillaFlicking?.destroy();
82
- }
83
-
84
- public beforeMount() {
131
+ },
132
+ beforeMount() {
85
133
  this.fillKeys();
86
- }
87
-
88
- public beforeUpdate() {
134
+ },
135
+ beforeUpdate() {
89
136
  this.fillKeys();
90
137
 
91
138
  this.diffResult = this.slotDiffer.update(this.getSlots());
92
- }
93
-
94
- public updated() {
139
+ },
140
+ updated() {
95
141
  const flicking = this.vanillaFlicking;
96
142
  const diffResult = this.diffResult;
97
143
 
@@ -109,9 +155,8 @@ class Flicking extends Vue.with(FlickingProps) {
109
155
  }
110
156
 
111
157
  this.diffResult = undefined;
112
- }
113
-
114
- public render() {
158
+ },
159
+ render() {
115
160
  const flicking = this.vanillaFlicking;
116
161
  const options = this.options;
117
162
  const initialized = flicking && flicking.initialized;
@@ -146,117 +191,61 @@ class Flicking extends Vue.with(FlickingProps) {
146
191
  return h(this.viewportTag, viewportData,
147
192
  [h(this.cameraTag, cameraData, { default: panels }), ...viewportSlots]
148
193
  );
149
- }
150
-
151
- public getSlots() {
152
- const slots = this.$slots.default
153
- ? this.$slots.default()
154
- : [];
155
-
156
- return slots.reduce((elementSlots, slot) => [...elementSlots, ...this.getElementVNodes(slot)], [] as VNode[]);
157
- }
158
-
159
- public getElementVNodes(slot: VNode, childSlots: VNode[] = []): VNode[] {
160
- if (slot.type === Fragment && Array.isArray(slot.children)) {
161
- slot.children
162
- .filter(child => child && typeof child === "object")
163
- .forEach(child => this.getElementVNodes(child as VNode, childSlots));
164
- } else {
165
- childSlots.push(slot);
166
- }
167
-
168
- return childSlots;
169
- }
170
-
171
- private bindEvents() {
172
- const flicking = this.vanillaFlicking;
173
- const events = (Object.keys(EVENTS) as Array<keyof typeof EVENTS>)
174
- .map(key => EVENTS[key]);
175
-
176
- events.forEach(eventName => {
177
- flicking!.on(eventName, (e: any) => {
178
- e.currentTarget = this;
179
- // Make events from camelCase to kebab-case
180
- this.$emit(eventName.replace(/([A-Z])/g, "-$1").toLowerCase(), e);
181
- });
182
- });
183
- }
184
-
185
- private checkPlugins() {
186
- const { list, added, removed, prevList } = this.pluginsDiffer.update(this.plugins);
187
-
188
- this.vanillaFlicking!.addPlugins(...added.map(index => list[index]));
189
- this.vanillaFlicking!.removePlugins(...removed.map(index => prevList[index]));
190
- }
191
-
192
- private fillKeys() {
193
- const vnodes = this.getSlots();
194
-
195
- vnodes.forEach((node, idx) => {
196
- if (node.key == null) {
197
- node.key = `$_${idx}`;
194
+ },
195
+ methods: {
196
+ getSlots() {
197
+ const slots = this.$slots.default
198
+ ? this.$slots.default()
199
+ : [];
200
+
201
+ return slots.reduce((elementSlots, slot) => [...elementSlots, ...this.getElementVNodes(slot)], [] as VNode[]);
202
+ },
203
+ getElementVNodes(slot: VNode, childSlots: VNode[] = []): VNode[] {
204
+ if (slot.type === Fragment && Array.isArray(slot.children)) {
205
+ slot.children
206
+ .filter(child => child && typeof child === "object")
207
+ .forEach(child => this.getElementVNodes(child as VNode, childSlots));
208
+ } else {
209
+ childSlots.push(slot);
198
210
  }
199
- });
200
- }
201
211
 
202
- private getChildren() {
203
- const childRefs = this.$refs;
212
+ return childSlots;
213
+ },
214
+ bindEvents() {
215
+ const flicking = this.vanillaFlicking;
216
+ const events = (Object.keys(EVENTS) as Array<keyof typeof EVENTS>)
217
+ .map(key => EVENTS[key]);
218
+
219
+ events.forEach(eventName => {
220
+ flicking.on(eventName, (e: any) => {
221
+ e.currentTarget = this;
222
+ // Make events from camelCase to kebab-case
223
+ this.$emit(eventName.replace(/([A-Z])/g, "-$1").toLowerCase(), e);
224
+ });
225
+ });
226
+ },
227
+ checkPlugins() {
228
+ const { list, added, removed, prevList } = this.pluginsDiffer.update(this.plugins);
229
+
230
+ this.vanillaFlicking!.addPlugins(...added.map(index => list[index]));
231
+ this.vanillaFlicking!.removePlugins(...removed.map(index => prevList[index]));
232
+ },
233
+ fillKeys() {
234
+ const vnodes = this.getSlots();
235
+
236
+ vnodes.forEach((node, idx) => {
237
+ if (node.key == null) {
238
+ node.key = `$_${idx}`;
239
+ }
240
+ });
241
+ },
242
+ getChildren() {
243
+ const childRefs = this.$refs;
204
244
 
205
- return Object.keys(childRefs).map(refKey => childRefs[refKey]);
245
+ return Object.keys(childRefs).map(refKey => childRefs[refKey]);
246
+ }
206
247
  }
248
+ }) as unknown as VueFlicking;
207
249
 
208
- private getPanels = () => {
209
- const componentInstance = getCurrentInstance() as unknown as { ctx: Flicking } | null;
210
- const vueFlicking = componentInstance?.ctx;
211
- const flicking = this.vanillaFlicking;
212
- const defaultSlots = this.getSlots();
213
- const diffResult = vueFlicking?.diffResult;
214
-
215
- const slots = diffResult
216
- ? getRenderingPanels(flicking!, diffResult)
217
- : defaultSlots;
218
-
219
- const panelComponent = resolveComponent("Panel");
220
- const panels = slots.map((slot, idx) => h(panelComponent as any, {
221
- key: slot.key!,
222
- ref: idx.toString()
223
- }, () => slot));
224
-
225
- return panels;
226
- };
227
-
228
- private getVirtualPanels = () => {
229
- const options = this.options;
230
- const {
231
- panelClass = "flicking-panel"
232
- } = options.virtual!;
233
- const panelsPerView = options.panelsPerView!;
234
- const flicking = this.vanillaFlicking;
235
- const initialized = flicking && flicking.initialized;
236
-
237
- const renderingIndexes = initialized
238
- ? flicking.renderer.strategy.getRenderingIndexesByOrder(flicking)
239
- : range(panelsPerView + 1);
240
-
241
- const firstPanel = initialized && flicking.panels[0];
242
- const size = firstPanel
243
- ? flicking.horizontal
244
- ? { width: firstPanel.size }
245
- : { height: firstPanel.size }
246
- : {};
247
-
248
- return renderingIndexes.map(idx => h("div", {
249
- key: idx,
250
- ref: idx.toString(),
251
- class: panelClass,
252
- style: size,
253
- domProps: {
254
- "data-element-index": idx
255
- }
256
- }));
257
- };
258
- }
259
-
260
- // eslint-disable-next-line @typescript-eslint/no-empty-interface
261
- interface Flicking extends VanillaFlicking, VueConstructor<Vue & FlickingProps> {}
250
+ interface Flicking extends VueFlicking, VanillaFlicking {}
262
251
  export default Flicking;
@@ -2,22 +2,41 @@
2
2
  * Copyright (c) 2015 NAVER Corp.
3
3
  * egjs projects are licensed under the MIT license
4
4
  */
5
- /* eslint-disable @typescript-eslint/explicit-member-accessibility */
6
- import { prop } from "vue-class-component";
7
- import {
8
- FlickingOptions,
9
- Plugin,
10
- Status
11
- } from "@egjs/flicking";
5
+ import { PropType } from "vue";
6
+ import { FlickingOptions, Plugin, Status } from "@egjs/flicking";
12
7
 
13
- class FlickingProps {
14
- viewportTag = prop<string>({ required: false, default: "div" });
15
- cameraTag = prop<string>({ required: false, default: "div" });
16
- hideBeforeInit = prop<boolean>({ required: false, default: false });
17
- firstPanelSize = prop<string>({ required: false, default: null });
18
- options = prop<Partial<FlickingOptions>>({ required: false, default: {} });
19
- plugins = prop<Plugin[]>({ required: false, default: [] });
20
- status = prop<Status>({ required: false, default: null });
21
- }
22
-
23
- export default FlickingProps;
8
+ export default {
9
+ viewportTag: {
10
+ type: String,
11
+ default: "div",
12
+ required: false
13
+ },
14
+ cameraTag: {
15
+ type: String,
16
+ default: "div",
17
+ required: false
18
+ },
19
+ hideBeforeInit: {
20
+ type: Boolean,
21
+ default: false,
22
+ required: false
23
+ },
24
+ firstPanelSize: {
25
+ type: String,
26
+ required: false
27
+ },
28
+ options: {
29
+ type: Object as unknown as () => Partial<FlickingOptions>,
30
+ default: () => ({}),
31
+ required: false
32
+ },
33
+ plugins: {
34
+ type: Array as PropType<Plugin[]>,
35
+ default: () => ([]),
36
+ required: false
37
+ },
38
+ status: {
39
+ type: Object as PropType<Status>,
40
+ required: false
41
+ }
42
+ };
@@ -2,11 +2,11 @@
2
2
  * Copyright (c) 2015 NAVER Corp.
3
3
  * egjs projects are licensed under the MIT license
4
4
  */
5
- import { ExternalElementProvider } from "@egjs/flicking";
5
+ import { ElementProvider } from "@egjs/flicking";
6
6
 
7
7
  import VuePanel from "./VuePanel";
8
8
 
9
- class VueElementProvider implements ExternalElementProvider {
9
+ class VueElementProvider implements ElementProvider {
10
10
  private _el: VuePanel;
11
11
  private _cachedElement: HTMLElement;
12
12
 
package/src/VuePanel.ts CHANGED
@@ -2,17 +2,23 @@
2
2
  * Copyright (c) 2015 NAVER Corp.
3
3
  * egjs projects are licensed under the MIT license
4
4
  */
5
- import { Vue } from "vue-class-component";
6
5
 
7
- class VuePanel extends Vue {
8
- public hide: boolean = false;
6
+ import { defineComponent } from "vue";
9
7
 
10
- public render() {
8
+ const VuePanel = defineComponent({
9
+ data() {
10
+ return {
11
+ hide: false
12
+ };
13
+ },
14
+ render() {
11
15
  if (this.hide || !this.$slots.default) return;
12
16
 
13
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
14
- return this.$slots.default() as any;
17
+ return this.$slots.default();
15
18
  }
16
- }
19
+ });
17
20
 
21
+ type VuePanelType = InstanceType<typeof VuePanel>;
22
+
23
+ interface VuePanel extends VuePanelType {}
18
24
  export default VuePanel;
package/src/types.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { ComponentOptionsMixin, DefineComponent, VNode } from "vue";
2
+ import VanillaFlicking, {
3
+ Plugin,
4
+ Status,
5
+ FlickingOptions,
6
+ FlickingEvents
7
+ } from "@egjs/flicking";
8
+ import Component from "@egjs/component";
9
+ import ListDiffer, { DiffResult } from "@egjs/list-differ";
10
+
11
+ import FlickingProps from "./FlickingProps";
12
+
13
+ export interface FlickingData {
14
+ renderEmitter: Component<{ render: void }>;
15
+ vanillaFlicking: VanillaFlicking;
16
+ pluginsDiffer: ListDiffer<Plugin>;
17
+ slotDiffer: ListDiffer<VNode>;
18
+ diffResult: DiffResult<VNode> | null;
19
+ }
20
+
21
+ type VueFlickingEmits = {
22
+ [key in keyof FlickingEvents]: (evt: FlickingEvents[key]) => any;
23
+ };
24
+
25
+ /* eslint-disable @typescript-eslint/indent */
26
+ export type VueFlicking = DefineComponent<
27
+ typeof FlickingProps,
28
+ // RawBindings
29
+ unknown,
30
+ // Data
31
+ FlickingData,
32
+ // Computed
33
+ {},
34
+ // Methods
35
+ {},
36
+ // Mixin
37
+ ComponentOptionsMixin,
38
+ // Extends
39
+ ComponentOptionsMixin,
40
+ // Emits
41
+ VueFlickingEmits,
42
+ string,
43
+ // Public Props
44
+ import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps,
45
+ // Props
46
+ Readonly<{
47
+ viewportTag?: unknown;
48
+ cameraTag?: unknown;
49
+ hideBeforeInit?: unknown;
50
+ firstPanelSize?: unknown;
51
+ options?: unknown;
52
+ plugins?: unknown;
53
+ status?: unknown;
54
+ } & {
55
+ viewportTag: string;
56
+ cameraTag: string;
57
+ hideBeforeInit: boolean;
58
+ options: Partial<FlickingOptions>;
59
+ plugins: Plugin[];
60
+ } & {
61
+ firstPanelSize?: string;
62
+ status?: Status;
63
+ } & {
64
+ [K in keyof VueFlickingEmits as `on${Capitalize<K>}`]?: VueFlickingEmits[K];
65
+ }>,
66
+ // Defaults
67
+ {
68
+ viewportTag: string;
69
+ cameraTag: string;
70
+ hideBeforeInit: boolean;
71
+ options: Partial<FlickingOptions>;
72
+ plugins: Plugin[];
73
+ }
74
+ >;
75
+ /* eslint-enable @typescript-eslint/indent */