mirror of
https://github.com/Pomax/BezierInfo-2.git
synced 2025-08-29 02:59:58 +02:00
renamed graphics-element dir
This commit is contained in:
37
docs/js/graphics-element/lib/perform-code-surgery.js
Normal file
37
docs/js/graphics-element/lib/perform-code-surgery.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import { GraphicsAPI } from "../api/graphics-api.js";
|
||||
|
||||
export default function performCodeSurgery(code) {
|
||||
// 0. strip out superfluous whitespace
|
||||
|
||||
code = code.replace(/\r?\n(\r?\n)+/, `\n`);
|
||||
|
||||
// 1. ensure that anything that needs to run by first calling its super function, does so.
|
||||
|
||||
GraphicsAPI.superCallers.forEach((name) => {
|
||||
const re = new RegExp(`${name}\\(([^)]*)\\)[\\s\\r\\n]*{[\\s\\r\\n]*`, `g`);
|
||||
code = code.replace(re, `${name}($1) { super.${name}($1);\n`);
|
||||
});
|
||||
|
||||
// 2. rewrite event handlers so that they capture the event and forward it to the super function.
|
||||
|
||||
GraphicsAPI.eventHandlers.forEach((name) => {
|
||||
const re = new RegExp(`\\b${name}\\(\\)[\\s\\r\\n]*{[\\s\\r\\n]*`, `g`);
|
||||
code = code.replace(re, `${name}(evt) { super.${name}(evt);\n`);
|
||||
});
|
||||
|
||||
// 3. rewrite all public GraphicsAPI functions to have the required `this.` prefix
|
||||
|
||||
GraphicsAPI.methods.forEach((fn) => {
|
||||
const re = new RegExp(`([!({\\s\\r\\n])${fn}\\(`, `g`);
|
||||
code = code.replace(re, `$1this.${fn}(`);
|
||||
});
|
||||
|
||||
// 4. do the same for all GraphicsAPI constants.
|
||||
|
||||
GraphicsAPI.constants.forEach((name) => {
|
||||
const re = new RegExp(`(\\b)${name}(\\b)`, `g`);
|
||||
code = code.replace(re, `$1this.${name}$2`);
|
||||
});
|
||||
|
||||
return code;
|
||||
}
|
Reference in New Issue
Block a user