UNPKG

7.11 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11};
12var _DuoyunSelectionBoxElement_getMode, _DuoyunSelectionBoxElement_onPointerDown, _DuoyunSelectionBoxElement_onPointerUp, _DuoyunSelectionBoxElement_onPointerMove;
13import { adoptedStyle, customElement, emitter, state } from '@mantou/gem/lib/decorators';
14import { GemElement, html } from '@mantou/gem/lib/element';
15import { createCSSSheet, css, styleMap } from '@mantou/gem/lib/utils';
16import { theme } from '../lib/theme';
17import './reflect';
18const style = createCSSSheet(css `
19 :host(:where(:not([hidden]))) {
20 display: contents;
21 }
22`);
23/**
24 * @customElement dy-selection-box
25 */
26let DuoyunSelectionBoxElement = class DuoyunSelectionBoxElement extends GemElement {
27 constructor() {
28 super(...arguments);
29 this.state = {
30 rect: {
31 left: 0,
32 right: 0,
33 bottom: 0,
34 top: 0,
35 width: 0,
36 height: 0,
37 },
38 mode: 'new',
39 };
40 _DuoyunSelectionBoxElement_getMode.set(this, ({ shiftKey, metaKey, ctrlKey }) => {
41 if (!shiftKey && !metaKey && !ctrlKey)
42 return 'new';
43 if (shiftKey)
44 return 'append';
45 return 'delete';
46 });
47 _DuoyunSelectionBoxElement_onPointerDown.set(this, (evt) => {
48 var _a;
49 if (evt.altKey)
50 return;
51 (_a = document.getSelection()) === null || _a === void 0 ? void 0 : _a.removeAllRanges();
52 this.selecting = true;
53 this.setState({ start: [evt.x, evt.y], mode: __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_getMode, "f").call(this, evt) });
54 addEventListener('pointermove', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerMove, "f"));
55 addEventListener('pointerup', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerUp, "f"));
56 addEventListener('pointercancel', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerUp, "f"));
57 addEventListener('contextmenu', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerUp, "f"));
58 });
59 _DuoyunSelectionBoxElement_onPointerUp.set(this, () => {
60 this.selecting = false;
61 this.setState({ start: undefined, stop: undefined });
62 removeEventListener('pointermove', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerMove, "f"));
63 removeEventListener('pointerup', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerUp, "f"));
64 removeEventListener('pointercancel', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerUp, "f"));
65 removeEventListener('contextmenu', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerUp, "f"));
66 });
67 _DuoyunSelectionBoxElement_onPointerMove.set(this, (evt) => {
68 const start = this.state.start;
69 const x = evt.x === start[0] ? evt.x + 0.01 : evt.x;
70 const y = evt.y === start[1] ? evt.y + 0.01 : evt.y;
71 const rect = {
72 left: Math.min(start[0], x),
73 right: Math.max(start[0], x),
74 top: Math.min(start[1], y),
75 bottom: Math.max(start[1], y),
76 width: Math.abs(start[0] - x),
77 height: Math.abs(start[1] - y),
78 };
79 if (Math.sqrt(rect.width ** 2 + rect.height ** 2) < 4)
80 return;
81 this.setState({ stop: [x, y], rect });
82 this.change({ rect, mode: this.state.mode });
83 });
84 this.mounted = () => {
85 const root = this.getRootNode();
86 root.addEventListener('pointerdown', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerDown, "f"));
87 return () => {
88 root.removeEventListener('pointerdown', __classPrivateFieldGet(this, _DuoyunSelectionBoxElement_onPointerDown, "f"));
89 };
90 };
91 this.render = () => {
92 const { start, stop, rect } = this.state;
93 if (!start || !stop)
94 return html ``;
95 const { left, top, width, height } = rect;
96 return html `
97 <dy-reflect .target=${document.body}>
98 <dy-selection-box-mask
99 style=${styleMap({
100 left: left + 'px',
101 top: top + 'px',
102 width: width + 'px',
103 height: height + 'px',
104 })}
105 ></dy-selection-box-mask>
106 </dy-reflect>
107 `;
108 };
109 }
110};
111_DuoyunSelectionBoxElement_getMode = new WeakMap(), _DuoyunSelectionBoxElement_onPointerDown = new WeakMap(), _DuoyunSelectionBoxElement_onPointerUp = new WeakMap(), _DuoyunSelectionBoxElement_onPointerMove = new WeakMap();
112__decorate([
113 emitter
114], DuoyunSelectionBoxElement.prototype, "change", void 0);
115__decorate([
116 state
117], DuoyunSelectionBoxElement.prototype, "selecting", void 0);
118DuoyunSelectionBoxElement = __decorate([
119 customElement('dy-selection-box'),
120 adoptedStyle(style)
121], DuoyunSelectionBoxElement);
122export { DuoyunSelectionBoxElement };
123const borderWidth = 10;
124const maskStyle = createCSSSheet(css `
125 :host {
126 position: fixed;
127 border: ${borderWidth}px solid transparent;
128 transform: translate(-${borderWidth}px, -${borderWidth}px);
129 }
130 :host::after,
131 :host::before {
132 content: '';
133 display: block;
134 position: absolute;
135 width: 100%;
136 height: 100%;
137 box-sizing: border-box;
138 }
139 :host::before {
140 opacity: 0.4;
141 background: ${theme.informativeColor};
142 }
143 :host::after {
144 border: 1px solid ${theme.informativeColor};
145 }
146`);
147/**
148 * @customElement dy-selection-box-mask
149 */
150let DuoyunSelectionBoxMaskElement = class DuoyunSelectionBoxMaskElement extends GemElement {
151};
152DuoyunSelectionBoxMaskElement = __decorate([
153 customElement('dy-selection-box-mask'),
154 adoptedStyle(maskStyle)
155], DuoyunSelectionBoxMaskElement);
156export { DuoyunSelectionBoxMaskElement };
157//# sourceMappingURL=selection-box.js.map
\No newline at end of file