snail.vue 1.0.10 → 1.0.11

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.
@@ -0,0 +1,6 @@
1
+ /* src:container\dynamic.vue index:0 */
2
+ .snail-dynamic-error{color:red}.snail-dynamic-error>span{color:gray}
3
+ /* src:prompt\loading.vue index:0 */
4
+ .snail-loading{display:block;height:100%;left:0;position:absolute;top:0;width:100%;z-index:10000}.snail-loading.show-mask{background-color:rgba(0,0,0,.15)}.snail-loading:after,.snail-loading:before{animation:snail-loading-stretch 1s ease-in-out infinite;border-radius:50%;content:"";display:block;display:inline-block;height:10px;left:50%;margin-left:-18px;margin-top:-6px;position:absolute;top:50%;width:10px}.snail-loading:before{background-color:#279bf1}.snail-loading:after{animation-delay:-.5s;background:#64d214;margin-left:0;margin-right:-18px}@keyframes snail-loading-stretch{0%,to{transform:scale(1)}50%{transform:scale(2)}}.snail-loading-enter-active,.snail-loading-leave-active{transition:opacity .5s ease-in-out}.snail-loading-enter-from,.snail-loading-leave-to{opacity:0}
5
+ /* src:container\components\dialog-wrapper.vue index:0 */
6
+ .snail-dialog{align-items:center;bottom:0;display:flex;justify-content:center;left:0;overflow:hidden;position:fixed;right:0;top:0}.snail-dialog:before{background-color:rgba(24,27,33,.45);content:"";height:100%;position:absolute;width:100%}.snail-dialog>.dialog-body{background-color:#fff;border-radius:4px;box-shadow:0 0 15px rgba(0,0,0,.3);box-sizing:border-box;position:relative}.snail-dialog.unactive:before{display:none!important}.snail-dialog.unactive>.dialog-body{box-shadow:0 0 4px rgba(0,0,0,.3)}.snail-dialog-enter-active,.snail-dialog-leave-active{transition:scale .1s ease-in-out,opacity .1s ease}.snail-dialog-enter-from{opacity:0;scale:.4}.snail-dialog-leave-to{opacity:0;scale:0}
@@ -1,2 +1,191 @@
1
+ import * as vue from 'vue';
2
+ import { App, Component } from 'vue';
3
+ import { IScope } from 'snail.core';
1
4
 
2
- export { };
5
+ /**
6
+ * Vue App助手类,做一些app实例的辅助性工作
7
+ */
8
+
9
+ /**
10
+ * app实例创建完之后的回调通知
11
+ * @param fn 回调通知
12
+ * @returns 通知句柄,可销毁回调通知,一般在外部销毁时执行
13
+ */
14
+ declare function onAppCreated(fn: (app: App) => void): IScope;
15
+ /**
16
+ * 触发app创建后事件
17
+ * @param app 创建的app实例
18
+ * @returns app自身
19
+ */
20
+ declare function triggerAppCreated(app: App): App;
21
+
22
+ /**
23
+ * 组件配置选项
24
+ */
25
+ type ComponentOptions = {
26
+ /**
27
+ * 组件名称;
28
+ * - 确保组件已注册,否则会加载不出来
29
+ */
30
+ name?: string;
31
+ /**
32
+ * Vue组件对象
33
+ * - name未传入时生效
34
+ */
35
+ component?: Component;
36
+ /**
37
+ * 组件js文件url地址
38
+ * - 支持#号锚点钻取
39
+ * - name、component未传入时生效
40
+ * - 推荐外部使用 shallowRef 包裹对象,避免响应式的性能问题
41
+ */
42
+ url?: string;
43
+ };
44
+
45
+ /**
46
+ * 模态弹窗显示的组件配置选项
47
+ */
48
+ type DialogOptions = ComponentOptions & {
49
+ /**
50
+ * 传递给组件的属性值,执行v-bind绑定到要显示的组件
51
+ * - key为属性名称,遵循vue解析规则
52
+ * - 若为事件监听,则使用onXXX
53
+ */
54
+ props?: Record<string, any>;
55
+ /**
56
+ * 按下esc健时是否关闭弹窗
57
+ * - 默认值:false
58
+ */
59
+ closeOnEscape?: boolean;
60
+ /**
61
+ * 点击遮罩层时是否关闭弹窗
62
+ * - 默认值:false
63
+ */
64
+ closeOnMask?: boolean;
65
+ /**
66
+ * 自定义class
67
+ * - 绑定模块弹窗显示的组件根元素上
68
+ */
69
+ class?: string | string[];
70
+ /**
71
+ * 自定义style
72
+ * - 绑定模块弹窗显示的组件根元素上
73
+ */
74
+ style?: string | string[];
75
+ /**
76
+ * 弹窗的z-index值
77
+ * - 无特殊情况,建议不粗韩,内部会自动生成,确保弹窗正确性
78
+ */
79
+ zIndex?: number;
80
+ /**
81
+ * 模态弹窗的自定义class
82
+ * - 绑定到模态弹窗的根元素上
83
+ */
84
+ rootClass?: string | string[];
85
+ };
86
+ /**
87
+ * 模块弹窗打开后的结果
88
+ * - 执行destroy方法时,强制关闭,不会执行onDialogClose
89
+ */
90
+ type DialogOpenResult<T> = Promise<T> & IScope;
91
+ /**
92
+ * 弹窗句柄:绑定给子组件使用
93
+ */
94
+ type DialogHandle<T> = {
95
+ /** 组件是否处于【弹窗】模式下 */
96
+ inDialog: boolean;
97
+ /**
98
+ * 关闭弹窗的方法
99
+ * @param data 关闭时传递数据
100
+ */
101
+ closeDialog(data?: T): void;
102
+ /**
103
+ * 注册监听【弹窗关闭的方法】
104
+ * - 仅支持注册一次,多次注册以最后一次的为准
105
+ * @param fn 关闭时执行的钩子函数,支持异步,返回false时将阻止弹窗关闭
106
+ */
107
+ onDialogClose(fn: () => false | undefined | Promise<false | undefined>): void;
108
+ };
109
+ /**
110
+ * 模态弹窗;配合【../components/dialog-wrapper.vue】使用
111
+ */
112
+ type Dialog = {
113
+ /**
114
+ * 弹窗Id,唯一值
115
+ */
116
+ id: string;
117
+ /**
118
+ * 模态弹窗显示的组件配置选项
119
+ */
120
+ options: DialogOptions;
121
+ /**
122
+ * 弹窗句柄
123
+ * - 提供关闭弹窗等操作
124
+ * - 将挂载到内容组件的属性上,方便使用
125
+ */
126
+ handle: DialogHandle<any>;
127
+ };
128
+
129
+ declare var __VLS_8: string | number;
130
+ declare var __VLS_9: any;
131
+ type __VLS_Slots$1 = {} & {
132
+ [K in NonNullable<typeof __VLS_8>]?: (props: typeof __VLS_9) => any;
133
+ };
134
+ declare const __VLS_component$1: vue.DefineComponent<ComponentOptions, {}, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<ComponentOptions> & Readonly<{}>, {}, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
135
+ declare const _default$1: __VLS_WithSlots$1<typeof __VLS_component$1, __VLS_Slots$1>;
136
+
137
+ type __VLS_WithSlots$1<T, S> = T & {
138
+ new (): {
139
+ $slots: S;
140
+ };
141
+ };
142
+
143
+ /**
144
+ * 打开模态弹窗
145
+ * @param options 弹窗组件配置选项
146
+ * @param onDestroyed 监听【调用方】的销毁时机,用于自动销毁打开的弹窗
147
+ * @returns 弹窗打开结果,可手动关闭弹窗
148
+ */
149
+ declare function openDialog<T>(options: DialogOptions, onDestroyed?: (fn: () => void) => void): DialogOpenResult<T>;
150
+
151
+ /**
152
+ * loading提示框的配置选项
153
+ */
154
+ type LoadingOptions = {
155
+ /**
156
+ * 是否显示loading效果
157
+ * - 默认值:false
158
+ */
159
+ show: boolean;
160
+ /**
161
+ * 展示遮罩层
162
+ * - 默认值:false,显示遮罩层
163
+ */
164
+ disabledMask?: boolean;
165
+ /**
166
+ * loading的根样式
167
+ * - 外部传入后,可进行自由定制loading样式
168
+ */
169
+ rootClass?: string | string[];
170
+ /**
171
+ * loading组件显示隐藏的动画名
172
+ * - 默认:snail-loading
173
+ */
174
+ transition?: string;
175
+ };
176
+
177
+ declare var __VLS_5: {};
178
+ type __VLS_Slots = {} & {
179
+ default?: (props: typeof __VLS_5) => any;
180
+ };
181
+ declare const __VLS_component: vue.DefineComponent<LoadingOptions, {}, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<LoadingOptions> & Readonly<{}>, {}, {}, {}, {}, string, vue.ComponentProvideOptions, false, {}, any>;
182
+ declare const _default: __VLS_WithSlots<typeof __VLS_component, __VLS_Slots>;
183
+
184
+ type __VLS_WithSlots<T, S> = T & {
185
+ new (): {
186
+ $slots: S;
187
+ };
188
+ };
189
+
190
+ export { _default$1 as SnailDynamic, _default as SnailLoading, onAppCreated, openDialog, triggerAppCreated };
191
+ export type { Dialog, DialogHandle, DialogOpenResult, DialogOptions, LoadingOptions };
package/dist/snail.vue.js CHANGED
@@ -1,7 +1,268 @@
1
1
  //@ sourceURL=/snail.vue.js
2
- (function () {
3
- 'use strict';
2
+ define(['exports', 'snail.core', 'vue'], (function (exports, snail_core, vue) { 'use strict';
4
3
 
4
+ const appCreatedFns = [];
5
+ function onAppCreated(fn) {
6
+ snail_core.mustFunction(fn, "fn");
7
+ appCreatedFns.push(fn);
8
+ return {
9
+ destroy: () => {
10
+ const index = appCreatedFns.indexOf(fn);
11
+ index >= 0 && appCreatedFns.splice(index, 1);
12
+ }
13
+ };
14
+ }
15
+ function triggerAppCreated(app) {
16
+ appCreatedFns.forEach(fn => fn(app));
17
+ return app;
18
+ }
5
19
 
20
+ var _sfc_main$2 = vue.defineComponent({
21
+ ...{
22
+ name: "SnailLoading",
23
+ inheritAttrs: false
24
+ },
25
+ __name: "loading",
26
+ props: {
27
+ show: {
28
+ type: Boolean,
29
+ default: false
30
+ },
31
+ disabledMask: {
32
+ type: Boolean,
33
+ default: false
34
+ },
35
+ rootClass: {
36
+ default: () => []
37
+ },
38
+ transition: {
39
+ default: "snail-loading"
40
+ }
41
+ },
42
+ setup(__props) {
43
+ return (_ctx, _cache) => {
44
+ return vue.openBlock(), vue.createBlock(vue.Transition, {
45
+ name: _ctx.transition
46
+ }, {
47
+ default: vue.withCtx(() => [_ctx.show ? (vue.openBlock(), vue.createElementBlock("div", {
48
+ key: 0,
49
+ class: vue.normalizeClass(["snail-loading", {
50
+ "show-mask": _ctx.disabledMask != true
51
+ }, _ctx.rootClass])
52
+ }, [vue.renderSlot(_ctx.$slots, "default", vue.normalizeProps(vue.guardReactiveProps(_ctx.$attrs)))], 2)) : vue.createCommentVNode("", true)]),
53
+ _: 3
54
+ }, 8, ["name"]);
55
+ };
56
+ }
57
+ });
6
58
 
7
- })();
59
+ var _sfc_main$1 = vue.defineComponent({
60
+ ...{
61
+ name: "SnailDynamic",
62
+ inheritAttrs: false
63
+ },
64
+ __name: "dynamic",
65
+ props: {
66
+ name: {},
67
+ component: {},
68
+ url: {}
69
+ },
70
+ setup(__props) {
71
+ const componentRef = vue.ref(null);
72
+ const dynamicComponent = vue.shallowRef(void 0);
73
+ const dynamicError = vue.shallowRef(void 0);
74
+ async function buildDynamicComponent() {
75
+ dynamicComponent.value = void 0;
76
+ dynamicError.value = void 0;
77
+ if (snail_core.isStringNotEmpty(__props.name) == true) {
78
+ dynamicComponent.value = __props.name;
79
+ return;
80
+ }
81
+ if (snail_core.isObject(__props.component) == true) {
82
+ dynamicComponent.value = __props.component;
83
+ return;
84
+ } else if (snail_core.isStringNotEmpty(__props.url) == true) {
85
+ console.log("load dynamic component:", __props.url);
86
+ const task = snail_core.script.load(__props.url);
87
+ await snail_core.delay(200);
88
+ try {
89
+ const comp = await task;
90
+ snail_core.isObject(comp) || snail_core.isStringNotEmpty(comp) ? dynamicComponent.value = comp : dynamicError.value = `load component failed:return nulll or undefined. url:${__props.url}.`;
91
+ } catch (ex) {
92
+ dynamicComponent.value = void 0;
93
+ dynamicError.value = snail_core.getMessage(ex);
94
+ }
95
+ } else {
96
+ dynamicError.value = "load error: name component、url are all empty.";
97
+ }
98
+ }
99
+ {
100
+ vue.watch(() => __props.name, buildDynamicComponent);
101
+ vue.watch(() => __props.component, buildDynamicComponent);
102
+ vue.watch(() => __props.url, buildDynamicComponent);
103
+ buildDynamicComponent();
104
+ }
105
+ vue.onErrorCaptured((error, vm, info) => {
106
+ if (componentRef.value == null || vm == componentRef.value) {
107
+ console.error("动态加载组件报错,已拦截错误:", error.message, error);
108
+ return false;
109
+ }
110
+ });
111
+ vue.onActivated(() => console.log("onActivated"));
112
+ vue.onDeactivated(() => console.log("onDeactivated"));
113
+ return (_ctx, _cache) => {
114
+ return vue.openBlock(), vue.createElementBlock(vue.Fragment, null, [(vue.openBlock(), vue.createBlock(vue.resolveDynamicComponent(dynamicComponent.value), vue.mergeProps(_ctx.$attrs, {
115
+ ref_key: "componentRef",
116
+ ref: componentRef
117
+ }), vue.createSlots({
118
+ _: 2
119
+ }, [vue.renderList(_ctx.$slots, (_, name) => {
120
+ return {
121
+ name,
122
+ fn: vue.withCtx(slotData => [vue.renderSlot(_ctx.$slots, name, vue.normalizeProps(vue.guardReactiveProps(slotData)))])
123
+ };
124
+ })]), 1040)), dynamicError.value != void 0 ? (vue.openBlock(), vue.createElementBlock("div", vue.mergeProps({
125
+ key: 0,
126
+ class: "snail-dynamic-error"
127
+ }, _ctx.$attrs), [_cache[0] || (_cache[0] = vue.createTextVNode(" load component error:")), vue.createElementVNode("span", null, vue.toDisplayString(dynamicError.value), 1)], 16)) : dynamicComponent.value == void 0 ? (vue.openBlock(), vue.createBlock(_sfc_main$2, {
128
+ key: 1,
129
+ show: true,
130
+ "disabled-mask": true
131
+ })) : vue.createCommentVNode("", true)], 64);
132
+ };
133
+ }
134
+ });
135
+
136
+ var addLink = href => snail_core.style.register(href);
137
+
138
+ addLink("/css/snail.vue.vue.css");
139
+
140
+ const _hoisted_1 = ["onClick"];
141
+ var _sfc_main = vue.defineComponent({
142
+ ...{
143
+ name: "SnailDialogWrapper",
144
+ inheritAttrs: true
145
+ },
146
+ __name: "dialog-wrapper",
147
+ props: {
148
+ descriptors: {
149
+ type: Array,
150
+ required: true
151
+ }
152
+ },
153
+ setup(__props) {
154
+ function onMaskClick(dialog) {
155
+ const last = __props.descriptors[__props.descriptors.length - 1];
156
+ last === dialog && dialog.options.closeOnMask && dialog.handle.closeDialog(dialog);
157
+ }
158
+ addEventListener("keyup", event => {
159
+ if (event.key === "Escape" && __props.descriptors.length > 0) {
160
+ const dialog = __props.descriptors[__props.descriptors.length - 1];
161
+ dialog.options.closeOnEscape && dialog.handle.closeDialog(dialog);
162
+ }
163
+ });
164
+ return (_ctx, _cache) => {
165
+ return vue.openBlock(), vue.createBlock(vue.TransitionGroup, {
166
+ name: "snail-dialog"
167
+ }, {
168
+ default: vue.withCtx(() => [(vue.openBlock(true), vue.createElementBlock(vue.Fragment, null, vue.renderList(__props.descriptors, (dialog, $index) => {
169
+ return vue.openBlock(), vue.createElementBlock("div", {
170
+ key: dialog.id,
171
+ class: vue.normalizeClass(["snail-dialog", dialog.options.rootClass, {
172
+ "unactive": __props.descriptors.length - 1 !== $index
173
+ }]),
174
+ style: vue.normalizeStyle("z-index:" + dialog.options.zIndex),
175
+ onClick: vue.withModifiers($event => onMaskClick(dialog), ["self"])
176
+ }, [vue.createVNode(_sfc_main$1, vue.mergeProps({
177
+ class: ["dialog-body", dialog.options.class],
178
+ name: dialog.options.name,
179
+ component: dialog.options.component,
180
+ url: dialog.options.url,
181
+ style: dialog.options.style
182
+ }, {
183
+ ref_for: true
184
+ }, dialog.options.props, {
185
+ ref_for: true
186
+ }, dialog.handle), null, 16, ["name", "component", "url", "class", "style"])], 14, _hoisted_1);
187
+ }), 128))]),
188
+ _: 1
189
+ });
190
+ };
191
+ }
192
+ });
193
+
194
+ function openDialog(options, onDestroyed) {
195
+ snail_core.mustObject(options, "options");
196
+ options.zIndex = options.zIndex || 2e3;
197
+ const deferred = snail_core.defer();
198
+ const dialog = {
199
+ id: snail_core.newId(),
200
+ options,
201
+ handle: Object.freeze({
202
+ inDialog: true,
203
+ async closeDialog(data) {
204
+ const hookCode = createDialogHookCode(dialog);
205
+ const rt = await dialogHook.runHookAsync(hookCode, {
206
+ mode: "one",
207
+ order: "desc"
208
+ });
209
+ if (rt.success != true) {
210
+ console.warn("run onDialogClose failed", rt.reason, rt.ex);
211
+ return;
212
+ }
213
+ destroyDialog(dialog);
214
+ deferred.resolve(data);
215
+ },
216
+ onDialogClose(fn) {
217
+ const hookCode = createDialogHookCode(dialog);
218
+ dialogHook.register(hookCode, fn);
219
+ }
220
+ })
221
+ };
222
+ initVueApp();
223
+ descriptors.value.push(dialog);
224
+ snail_core.isFunction(onDestroyed) && onDestroyed(() => destroyDialog(dialog));
225
+ const dr = deferred.promise;
226
+ dr.destroy = () => {
227
+ console.warn("force close dialog after openDialog caller called close function.");
228
+ destroyDialog(dialog);
229
+ deferred.resolve(void 0);
230
+ };
231
+ return dr;
232
+ }
233
+ const descriptors = vue.ref([]);
234
+ const dialogHook = snail_core.hook.newScope();
235
+ const createDialogHookCode = dialog => `onCloseDialog:${dialog.id}`;
236
+ function destroyDialog(dialog) {
237
+ const index = descriptors.value.indexOf(dialog);
238
+ if (index != -1) {
239
+ for (var tmpIndex = index; tmpIndex < descriptors.value.length; tmpIndex++) {
240
+ const hookCode = createDialogHookCode(descriptors.value[tmpIndex]);
241
+ dialogHook.remove(hookCode);
242
+ }
243
+ descriptors.value.splice(index);
244
+ }
245
+ }
246
+ const initVueApp = (() => {
247
+ var dialogApp = void 0;
248
+ return function () {
249
+ if (dialogApp == void 0) {
250
+ const container = document.createElement("div");
251
+ container.style = "height:0 !important; width:0 !important;";
252
+ container.classList.add("snail-dialog-container");
253
+ document.body.appendChild(container);
254
+ dialogApp = vue.createApp(_sfc_main, {
255
+ descriptors: descriptors.value
256
+ });
257
+ triggerAppCreated(dialogApp).mount(container);
258
+ }
259
+ };
260
+ })();
261
+
262
+ exports.SnailDynamic = _sfc_main$1;
263
+ exports.SnailLoading = _sfc_main$2;
264
+ exports.onAppCreated = onAppCreated;
265
+ exports.openDialog = openDialog;
266
+ exports.triggerAppCreated = triggerAppCreated;
267
+
268
+ }));
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "依赖【snail】,基于vue封装常用UI组件",
4
4
  "author": "snail_dev@163.com",
5
5
  "license": "MIT",
6
- "version": "1.0.10",
6
+ "version": "1.0.11",
7
7
  "type": "module",
8
8
  "main": "dist/index.js",
9
9
  "module": "dist/index.js",
@@ -11,21 +11,21 @@
11
11
  "scripts": {
12
12
  "build": "rollup -c ./rollup.config.js",
13
13
  "types": "vue-tsc -p ./tsconfig.dts.json --declarationDir ./dist/_types",
14
- "build:test": "rollup --watch -c ./test/rollup.config.js"
14
+ "dev:test": "rollup --watch -c ./test/rollup.config.js"
15
15
  },
16
16
  "dependencies": {
17
17
  "vue": "^3.5.14",
18
- "snail.core": ">=1.2.1"
18
+ "snail.core": ">=1.2.8"
19
19
  },
20
20
  "devDependencies": {
21
21
  "vue-tsc": "^2.2.10",
22
- "snail.rollup": ">=1.2.1",
22
+ "snail.rollup": ">=1.2.2",
23
23
  "snail.rollup-asset": ">=1.2.1",
24
24
  "snail.rollup-html": ">=1.2.1",
25
25
  "snail.rollup-url": ">=1.2.1",
26
26
  "snail.rollup-inject": ">=1.2.1",
27
27
  "snail.rollup-script": ">=1.2.1",
28
28
  "snail.rollup-style": ">=1.2.1",
29
- "snail.rollup-vue": ">=1.2.1"
29
+ "snail.rollup-vue": ">=1.2.3"
30
30
  }
31
31
  }