/
events.ts
114 lines (92 loc) · 2.97 KB
/
events.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* Tencent is pleased to support the open source community by making TMagicEditor available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* 通用的事件处理
*/
import App from './App';
import Node from './Node';
export interface EventOption {
label: string;
value: string;
}
const COMMON_EVENT_PREFIX = 'magic:common:events:';
const COMMON_METHOD_PREFIX = 'magic:common:actions:';
const CommonMethod = {
SHOW: 'show',
HIDE: 'hide',
SCROLL_TO_VIEW: 'scrollIntoView',
SCROLL_TO_TOP: 'scrollToTop',
};
export const DEFAULT_EVENTS: EventOption[] = [{ label: '点击', value: `${COMMON_EVENT_PREFIX}click` }];
export const DEFAULT_METHODS: EventOption[] = [];
export const getCommonEventName = (commonEventName: string) => {
if (commonEventName.startsWith(COMMON_EVENT_PREFIX)) return commonEventName;
return `${COMMON_EVENT_PREFIX}${commonEventName}`;
};
export const isCommonMethod = (methodName: string) => methodName.startsWith(COMMON_METHOD_PREFIX);
// 点击在组件内的某个元素上,需要向上寻找到当前组件
const getDirectComponent = (element: HTMLElement | null, app: App): Node | Boolean => {
if (!element) {
return false;
}
if (!element.id) {
return getDirectComponent(element.parentElement, app);
}
const node = app.page?.getNode(element.id);
if (!node) {
return false;
}
return node;
};
const commonClickEventHandler = (app: App, eventName: string, e: any) => {
const node = getDirectComponent(e.target, app);
if (node) {
app.emit(getCommonEventName(eventName), node);
}
};
export const bindCommonEventListener = (app: App) => {
window.document.body.addEventListener('click', (e: any) => {
commonClickEventHandler(app, 'click', e);
});
window.document.body.addEventListener(
'click',
(e: any) => {
commonClickEventHandler(app, 'click:capture', e);
},
true,
);
};
export const triggerCommonMethod = (methodName: string, node: Node) => {
const { instance } = node;
switch (methodName.replace(COMMON_METHOD_PREFIX, '')) {
case CommonMethod.SHOW:
instance.show();
break;
case CommonMethod.HIDE:
instance.hide();
break;
case CommonMethod.SCROLL_TO_VIEW:
instance.$el.scrollIntoView({ behavior: 'smooth' });
break;
case CommonMethod.SCROLL_TO_TOP:
window.scrollTo({ top: 0, behavior: 'smooth' });
break;
default:
break;
}
};