custom-rendrer 0.0.0-stage → 1.0.0
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/README.md +120 -2
- package/core/context.js +65 -0
- package/core/engine.js +130 -0
- package/core/registry.js +174 -0
- package/core/transformer.js +140 -0
- package/index.d.ts +212 -0
- package/index.js +68 -0
- package/input/event-mapper.js +87 -0
- package/input/input-adapter.js +65 -0
- package/output/output-adapter.js +66 -0
- package/package.json +101 -4
- package/plugins/babel.js +29 -0
- package/plugins/index.js +4 -0
- package/plugins/rollup.js +15 -0
- package/plugins/vite.js +88 -0
- package/plugins/webpack.js +82 -0
- package/rendering/element-manager.js +72 -0
- package/rendering/renderer.js +130 -0
- package/rendering/visual-layer.js +125 -0
- package/rules/config.js +55 -0
- package/rules/exclusions.js +78 -0
- package/rules/mappings.js +24 -0
- package/runtime/bootstrap.js +229 -0
- package/runtime/observer.js +98 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { engine } from '../core/engine.js';
|
|
2
|
+
import { VisualLayer } from '../rendering/visual-layer.js';
|
|
3
|
+
import { ElementManager } from '../rendering/element-manager.js';
|
|
4
|
+
import { AbstractionRenderer } from '../rendering/renderer.js';
|
|
5
|
+
import { RuntimeObserver } from './observer.js';
|
|
6
|
+
import { EventMapper } from '../input/event-mapper.js';
|
|
7
|
+
import { InputAdapter } from '../input/input-adapter.js';
|
|
8
|
+
import { OutputAdapter } from '../output/output-adapter.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Bootstrap & Runtime Facade
|
|
12
|
+
* Initializes and coordinates all browser runtime components.
|
|
13
|
+
*/
|
|
14
|
+
export class AbstractionRuntime {
|
|
15
|
+
constructor(options = {}) {
|
|
16
|
+
this.engine = options.engine || engine;
|
|
17
|
+
if (options.config) {
|
|
18
|
+
this.engine.configure(options.config);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
this.visualLayer = new VisualLayer();
|
|
22
|
+
this.elementManager = new ElementManager(this.visualLayer);
|
|
23
|
+
this.renderer = new AbstractionRenderer({
|
|
24
|
+
engine: this.engine,
|
|
25
|
+
visualLayer: this.visualLayer,
|
|
26
|
+
elementManager: this.elementManager,
|
|
27
|
+
});
|
|
28
|
+
this.observer = new RuntimeObserver(this.renderer);
|
|
29
|
+
this.eventMapper = new EventMapper(this.engine);
|
|
30
|
+
this.inputAdapter = new InputAdapter(this.engine);
|
|
31
|
+
this.outputAdapter = new OutputAdapter(this.engine, this.inputAdapter);
|
|
32
|
+
|
|
33
|
+
this.isInitialized = false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Starts the browser runtime, injects styles, and attaches observers & event listeners.
|
|
38
|
+
*/
|
|
39
|
+
start(rootElement = null) {
|
|
40
|
+
if (typeof window === 'undefined') return this;
|
|
41
|
+
if (this.isInitialized) return this;
|
|
42
|
+
|
|
43
|
+
// 1. Inject presentation stylesheet
|
|
44
|
+
this.visualLayer.injectStyles();
|
|
45
|
+
|
|
46
|
+
// 2. Load build-time rules injected by Babel/Vite plugin
|
|
47
|
+
if (typeof window !== 'undefined' && Array.isArray(window.__ABSTRACTION_BUILD_RULES__)) {
|
|
48
|
+
for (const rule of window.__ABSTRACTION_BUILD_RULES__) {
|
|
49
|
+
this.engine.registerRule(rule);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 3. Load user-configured mappings from localStorage
|
|
54
|
+
this.loadUserMappings();
|
|
55
|
+
|
|
56
|
+
// 4. Start DOM mutation observer
|
|
57
|
+
const targetRoot = rootElement || (typeof document !== 'undefined' ? document.body : null);
|
|
58
|
+
if (targetRoot) {
|
|
59
|
+
this.observer.start(targetRoot);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// 5. Attach interaction event mapper
|
|
63
|
+
this.eventMapper.attach(typeof document !== 'undefined' ? document : null);
|
|
64
|
+
|
|
65
|
+
this.isInitialized = true;
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Loads user-end mappings from localStorage.
|
|
71
|
+
*/
|
|
72
|
+
loadUserMappings() {
|
|
73
|
+
if (typeof window === 'undefined' || !window.localStorage) return;
|
|
74
|
+
try {
|
|
75
|
+
const raw = localStorage.getItem('frontend_abstraction_user_mappings');
|
|
76
|
+
if (raw) {
|
|
77
|
+
const mappings = JSON.parse(raw);
|
|
78
|
+
if (Array.isArray(mappings)) {
|
|
79
|
+
for (const m of mappings) {
|
|
80
|
+
this.engine.registerRule(m);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
} catch (e) {
|
|
85
|
+
console.warn('Failed to load user abstraction mappings:', e);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Adds or updates a word mapping directly from user-end and saves it to localStorage.
|
|
91
|
+
* Immediately updates visible screen elements without reloading.
|
|
92
|
+
*/
|
|
93
|
+
addMapping(original, display, options = {}) {
|
|
94
|
+
const rule = {
|
|
95
|
+
id: `user_${Date.now()}_${Math.random().toString(36).substring(2, 7)}`,
|
|
96
|
+
original,
|
|
97
|
+
display,
|
|
98
|
+
priority: options.priority || 20,
|
|
99
|
+
context: options.context || null,
|
|
100
|
+
enabled: true,
|
|
101
|
+
};
|
|
102
|
+
this.engine.registerRule(rule);
|
|
103
|
+
|
|
104
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
105
|
+
let current = this.getUserMappings();
|
|
106
|
+
// Remove any existing user mapping for the same original
|
|
107
|
+
current = current.filter(r => r.original !== original);
|
|
108
|
+
current.push(rule);
|
|
109
|
+
try {
|
|
110
|
+
localStorage.setItem('frontend_abstraction_user_mappings', JSON.stringify(current));
|
|
111
|
+
} catch (e) {
|
|
112
|
+
console.warn('Failed to persist user mapping:', e);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// Immediately re-scan and apply visual presentation
|
|
117
|
+
if (typeof document !== 'undefined' && document.body) {
|
|
118
|
+
this.renderer.scanAndRender(document.body);
|
|
119
|
+
}
|
|
120
|
+
return rule;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Gets all user-end mappings saved in localStorage.
|
|
125
|
+
*/
|
|
126
|
+
getUserMappings() {
|
|
127
|
+
if (typeof window === 'undefined' || !window.localStorage) return [];
|
|
128
|
+
try {
|
|
129
|
+
const raw = localStorage.getItem('frontend_abstraction_user_mappings');
|
|
130
|
+
return raw ? JSON.parse(raw) : [];
|
|
131
|
+
} catch {
|
|
132
|
+
return [];
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Clears all user-configured mappings from localStorage and resets DOM.
|
|
138
|
+
*/
|
|
139
|
+
clearUserMappings() {
|
|
140
|
+
if (typeof window !== 'undefined' && window.localStorage) {
|
|
141
|
+
localStorage.removeItem('frontend_abstraction_user_mappings');
|
|
142
|
+
}
|
|
143
|
+
if (typeof document !== 'undefined' && document.body) {
|
|
144
|
+
this.renderer.unrender(document.body);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Shuts down runtime and cleans up DOM.
|
|
150
|
+
*/
|
|
151
|
+
stop() {
|
|
152
|
+
this.observer.stop();
|
|
153
|
+
this.eventMapper.detach();
|
|
154
|
+
if (typeof document !== 'undefined' && document.body) {
|
|
155
|
+
this.renderer.unrender(document.body);
|
|
156
|
+
}
|
|
157
|
+
this.isInitialized = false;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Global runtime instance
|
|
162
|
+
export const runtime = new AbstractionRuntime();
|
|
163
|
+
|
|
164
|
+
// Auto-wire build-time rules via property descriptor on window
|
|
165
|
+
if (typeof window !== 'undefined') {
|
|
166
|
+
let _buildRules = window.__ABSTRACTION_BUILD_RULES__ || [];
|
|
167
|
+
if (Array.isArray(_buildRules) && _buildRules.length > 0) {
|
|
168
|
+
for (const rule of _buildRules) {
|
|
169
|
+
runtime.engine.registerRule(rule);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
Object.defineProperty(window, '__ABSTRACTION_BUILD_RULES__', {
|
|
175
|
+
configurable: true,
|
|
176
|
+
enumerable: true,
|
|
177
|
+
get() {
|
|
178
|
+
return _buildRules;
|
|
179
|
+
},
|
|
180
|
+
set(newRules) {
|
|
181
|
+
_buildRules = newRules;
|
|
182
|
+
if (Array.isArray(newRules)) {
|
|
183
|
+
console.log('[FrontendAbstraction] Received build rules:', newRules);
|
|
184
|
+
for (const rule of newRules) {
|
|
185
|
+
runtime.engine.registerRule(rule);
|
|
186
|
+
}
|
|
187
|
+
if (typeof document !== 'undefined' && document.body) {
|
|
188
|
+
runtime.renderer.scanAndRender(document.body);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
} catch (e) {
|
|
194
|
+
console.warn('[FrontendAbstraction] Could not define __ABSTRACTION_BUILD_RULES__ setter:', e);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Initialize helper
|
|
200
|
+
*/
|
|
201
|
+
export function initAbstractionRuntime(options = {}) {
|
|
202
|
+
const instance = new AbstractionRuntime(options);
|
|
203
|
+
if (typeof window !== 'undefined') {
|
|
204
|
+
instance.start();
|
|
205
|
+
window.__FRONTEND_ABSTRACTION__ = instance;
|
|
206
|
+
}
|
|
207
|
+
return instance;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Auto-bootstrap in browser if loaded directly
|
|
211
|
+
if (typeof window !== 'undefined' && !window.__FRONTEND_ABSTRACTION__) {
|
|
212
|
+
window.__FRONTEND_ABSTRACTION__ = runtime;
|
|
213
|
+
if (document.readyState === 'loading') {
|
|
214
|
+
document.addEventListener('DOMContentLoaded', () => runtime.start());
|
|
215
|
+
} else {
|
|
216
|
+
runtime.start();
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Re-scan whenever window loads to catch all lazy-loaded React components
|
|
221
|
+
if (typeof window !== 'undefined') {
|
|
222
|
+
window.addEventListener('load', () => {
|
|
223
|
+
if (window.__FRONTEND_ABSTRACTION__ && document.body) {
|
|
224
|
+
window.__FRONTEND_ABSTRACTION__.renderer.scanAndRender(document.body);
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export default runtime;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { AbstractionRenderer } from '../rendering/renderer.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* RuntimeObserver
|
|
5
|
+
* Monitors DOM mutations (React component updates, dynamic mounts, conditional rendering)
|
|
6
|
+
* and applies visual abstraction seamlessly.
|
|
7
|
+
*/
|
|
8
|
+
export class RuntimeObserver {
|
|
9
|
+
constructor(renderer = null, options = {}) {
|
|
10
|
+
this.renderer = renderer || new AbstractionRenderer();
|
|
11
|
+
this.observer = null;
|
|
12
|
+
this.isObserving = false;
|
|
13
|
+
this.options = {
|
|
14
|
+
debounceMs: options.debounceMs || 10,
|
|
15
|
+
root: options.root || null,
|
|
16
|
+
};
|
|
17
|
+
this._pendingNodes = new Set();
|
|
18
|
+
this._timer = null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Starts observing a DOM root container (defaults to document.body).
|
|
23
|
+
*/
|
|
24
|
+
start(targetRoot = null) {
|
|
25
|
+
if (typeof window === 'undefined' || typeof MutationObserver === 'undefined') {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const root = targetRoot || this.options.root || document.body;
|
|
30
|
+
if (!root || this.isObserving) return;
|
|
31
|
+
|
|
32
|
+
// Initial scan
|
|
33
|
+
this.renderer.scanAndRender(root);
|
|
34
|
+
|
|
35
|
+
this.observer = new MutationObserver((mutations) => {
|
|
36
|
+
this._handleMutations(mutations);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
this.observer.observe(root, {
|
|
40
|
+
childList: true,
|
|
41
|
+
subtree: true,
|
|
42
|
+
characterData: true,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
this.isObserving = true;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Stops observing mutations.
|
|
50
|
+
*/
|
|
51
|
+
stop() {
|
|
52
|
+
if (this.observer) {
|
|
53
|
+
this.observer.disconnect();
|
|
54
|
+
this.observer = null;
|
|
55
|
+
}
|
|
56
|
+
this.isObserving = false;
|
|
57
|
+
if (this._timer) {
|
|
58
|
+
clearTimeout(this._timer);
|
|
59
|
+
this._timer = null;
|
|
60
|
+
}
|
|
61
|
+
this._pendingNodes.clear();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
_handleMutations(mutations) {
|
|
65
|
+
for (const mutation of mutations) {
|
|
66
|
+
if (mutation.type === 'childList') {
|
|
67
|
+
for (const addedNode of mutation.addedNodes) {
|
|
68
|
+
if (addedNode.nodeType === 1) {
|
|
69
|
+
this._pendingNodes.add(addedNode);
|
|
70
|
+
} else if (addedNode.nodeType === 3 && addedNode.parentElement) {
|
|
71
|
+
this._pendingNodes.add(addedNode.parentElement);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
} else if (mutation.type === 'characterData' && mutation.target.parentElement) {
|
|
75
|
+
this._pendingNodes.add(mutation.target.parentElement);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this._scheduleFlush();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
_scheduleFlush() {
|
|
83
|
+
if (this._timer) return;
|
|
84
|
+
this._timer = setTimeout(() => {
|
|
85
|
+
this._flushPendingNodes();
|
|
86
|
+
this._timer = null;
|
|
87
|
+
}, this.options.debounceMs);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
_flushPendingNodes() {
|
|
91
|
+
for (const node of this._pendingNodes) {
|
|
92
|
+
if (node.isConnected) {
|
|
93
|
+
this.renderer.scanAndRender(node);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
this._pendingNodes.clear();
|
|
97
|
+
}
|
|
98
|
+
}
|