@editframe/elements 0.15.0-beta.1 → 0.15.0-beta.3

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,219 @@
1
+ import { LitElement, type ReactiveController } from "lit";
2
+
3
+ type Constructor<T = {}> = new (...args: any[]) => T;
4
+
5
+ // Symbol to identify elements that can be targeted
6
+ const EF_TARGETABLE = Symbol("EF_TARGETABLE");
7
+
8
+ class TargetRegistry {
9
+ private idMap = new Map<string, LitElement>();
10
+ private callbacks = new Map<
11
+ string,
12
+ Set<(target: LitElement | undefined) => void>
13
+ >();
14
+
15
+ subscribe(id: string, callback: (target: LitElement | undefined) => void) {
16
+ this.callbacks.set(id, this.callbacks.get(id) ?? new Set());
17
+ this.callbacks.get(id)?.add(callback);
18
+ }
19
+
20
+ unsubscribe(
21
+ id: string | null,
22
+ callback: (target: LitElement | undefined) => void,
23
+ ) {
24
+ if (id === null) {
25
+ return;
26
+ }
27
+ this.callbacks.get(id)?.delete(callback);
28
+ if (this.callbacks.get(id)?.size === 0) {
29
+ this.callbacks.delete(id);
30
+ }
31
+ }
32
+
33
+ get(id: string) {
34
+ return this.idMap.get(id);
35
+ }
36
+
37
+ register(id: string, target: LitElement) {
38
+ this.idMap.set(id, target);
39
+ for (const callback of this.callbacks.get(id) ?? []) {
40
+ callback(target);
41
+ }
42
+ }
43
+
44
+ unregister(id: string) {
45
+ for (const callback of this.callbacks.get(id) ?? []) {
46
+ callback(undefined);
47
+ }
48
+ this.idMap.delete(id);
49
+ this.callbacks.delete(id);
50
+ }
51
+ }
52
+
53
+ // Map of root nodes to their target registries
54
+ const documentRegistries = new WeakMap<Node, TargetRegistry>();
55
+
56
+ const getRegistry = (root: Node) => {
57
+ let registry = documentRegistries.get(root);
58
+ if (!registry) {
59
+ registry = new TargetRegistry();
60
+ documentRegistries.set(root, registry);
61
+ }
62
+ return registry;
63
+ };
64
+
65
+ export declare class TargetableMixinInterface {
66
+ id: string;
67
+ }
68
+
69
+ export const isEFTargetable = (obj: any): obj is TargetableMixinInterface =>
70
+ obj[EF_TARGETABLE];
71
+
72
+ export const EFTargetable = <T extends Constructor<LitElement>>(
73
+ superClass: T,
74
+ ) => {
75
+ class TargetableElement extends superClass {
76
+ #registry: TargetRegistry | null = null;
77
+
78
+ static get observedAttributes(): string[] {
79
+ // Get parent's observed attributes
80
+ const parentAttributes = (superClass as any).observedAttributes || [];
81
+ // Add 'id' if not already present
82
+ return [...new Set([...parentAttributes, "id"])];
83
+ }
84
+
85
+ private updateRegistry(oldValue: string, newValue: string) {
86
+ if (!this.#registry) return;
87
+ if (oldValue === newValue) return;
88
+
89
+ if (oldValue) {
90
+ this.#registry.unregister(oldValue);
91
+ }
92
+ if (newValue) {
93
+ this.#registry.register(newValue, this);
94
+ }
95
+ }
96
+
97
+ connectedCallback() {
98
+ super.connectedCallback();
99
+ this.#registry = getRegistry(this.getRootNode());
100
+ const initialId = this.getAttribute("id");
101
+ if (initialId) {
102
+ this.updateRegistry("", initialId);
103
+ }
104
+ }
105
+
106
+ attributeChangedCallback(
107
+ name: string,
108
+ old: string | null,
109
+ value: string | null,
110
+ ) {
111
+ super.attributeChangedCallback(name, old, value);
112
+ if (name === "id") {
113
+ this.updateRegistry(old ?? "", value ?? "");
114
+ }
115
+ }
116
+
117
+ disconnectedCallback() {
118
+ if (this.#registry) {
119
+ this.updateRegistry(this.id, "");
120
+ this.#registry = null;
121
+ }
122
+ super.disconnectedCallback();
123
+ }
124
+ }
125
+
126
+ Object.defineProperty(TargetableElement.prototype, EF_TARGETABLE, {
127
+ value: true,
128
+ });
129
+
130
+ return TargetableElement as T;
131
+ };
132
+
133
+ class TargetUpdateController implements ReactiveController {
134
+ constructor(private host: LitElement) {}
135
+
136
+ hostConnected() {
137
+ this.host.requestUpdate();
138
+ }
139
+
140
+ hostDisconnected() {
141
+ this.host.requestUpdate();
142
+ }
143
+
144
+ hostUpdate() {
145
+ this.host.requestUpdate();
146
+ }
147
+ }
148
+
149
+ export class TargetController implements ReactiveController {
150
+ private host: LitElement & { targetElement: Element | null; target: string };
151
+ private targetController: ReactiveController | null = null;
152
+ private currentTargetString: string | null = null;
153
+
154
+ constructor(
155
+ host: LitElement & { targetElement: Element | null; target: string },
156
+ ) {
157
+ this.host = host;
158
+ this.host.addController(this);
159
+ this.currentTargetString = this.host.target;
160
+ if (this.currentTargetString) {
161
+ this.registry.subscribe(this.currentTargetString, this.registryCallback);
162
+ }
163
+ }
164
+
165
+ private registryCallback = (target: LitElement | undefined) => {
166
+ this.host.targetElement = target ?? null;
167
+ };
168
+
169
+ private updateTarget() {
170
+ const newTarget = this.registry.get(this.host.target);
171
+ if (this.host.targetElement !== newTarget) {
172
+ this.disconnectFromTarget();
173
+ this.host.targetElement = newTarget ?? (null as Element | null);
174
+ this.connectToTarget();
175
+ }
176
+ }
177
+
178
+ private connectToTarget() {
179
+ if (this.host.targetElement instanceof LitElement) {
180
+ this.targetController = new TargetUpdateController(this.host);
181
+ this.host.targetElement.addController(this.targetController);
182
+ }
183
+ }
184
+
185
+ private disconnectFromTarget() {
186
+ if (
187
+ this.host.targetElement instanceof LitElement &&
188
+ this.targetController
189
+ ) {
190
+ this.host.targetElement.removeController(this.targetController);
191
+ this.targetController = null;
192
+ }
193
+ }
194
+
195
+ private get registry() {
196
+ const root = this.host.getRootNode();
197
+ return getRegistry(root);
198
+ }
199
+
200
+ hostDisconnected() {
201
+ this.disconnectFromTarget();
202
+ }
203
+
204
+ hostConnected() {
205
+ this.updateTarget();
206
+ }
207
+
208
+ hostUpdate() {
209
+ if (this.currentTargetString !== this.host.target) {
210
+ this.registry.unsubscribe(
211
+ this.currentTargetString,
212
+ this.registryCallback,
213
+ );
214
+ this.registry.subscribe(this.host.target, this.registryCallback);
215
+ this.updateTarget();
216
+ this.currentTargetString = this.host.target;
217
+ }
218
+ }
219
+ }
@@ -8,6 +8,16 @@ import { focusedElementContext } from "./focusedElementContext.js";
8
8
 
9
9
  @customElement("ef-preview")
10
10
  export class EFPreview extends ContextMixin(TWMixin(LitElement)) {
11
+ static styles = [
12
+ css`
13
+ :host {
14
+ position: relative;
15
+ display: block;
16
+ cursor: crosshair;
17
+ }
18
+ `,
19
+ ];
20
+
11
21
  @provide({ context: focusedElementContext })
12
22
  focusedElement?: HTMLElement;
13
23
 
@@ -37,15 +47,6 @@ export class EFPreview extends ContextMixin(TWMixin(LitElement)) {
37
47
  });
38
48
  }
39
49
 
40
- static styles = [
41
- css`
42
- :host {
43
- display: block;
44
- cursor: crosshair;
45
- }
46
- `,
47
- ];
48
-
49
50
  render() {
50
51
  return html`<slot></slot>`;
51
52
  }