-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: add custom widget to anvil #37875
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
(function (nativeConsole) { | ||||||||||||||||||||||||||||||||||||||||||||||
const postMessage = (method, args) => { | ||||||||||||||||||||||||||||||||||||||||||||||
window.parent.postMessage( | ||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||
type: "CUSTOM_WIDGET_CONSOLE_EVENT", | ||||||||||||||||||||||||||||||||||||||||||||||
data: { | ||||||||||||||||||||||||||||||||||||||||||||||
type: method, | ||||||||||||||||||||||||||||||||||||||||||||||
args: args.map((d) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||
message: d, | ||||||||||||||||||||||||||||||||||||||||||||||
})), | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
"*", | ||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
const createProxy = (method) => | ||||||||||||||||||||||||||||||||||||||||||||||
new Proxy(nativeConsole[method], { | ||||||||||||||||||||||||||||||||||||||||||||||
apply(target, _this, args) { | ||||||||||||||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||||||||||||||
postMessage(method, args); | ||||||||||||||||||||||||||||||||||||||||||||||
} finally { | ||||||||||||||||||||||||||||||||||||||||||||||
return Reflect.apply(target, _this, args); | ||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+17
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix the return statement in finally block The return statement in the finally block can override returns from the try block, potentially masking errors. const createProxy = (method) =>
new Proxy(nativeConsole[method], {
apply(target, _this, args) {
+ let result;
try {
postMessage(method, args);
+ result = Reflect.apply(target, _this, args);
} finally {
- return Reflect.apply(target, _this, args);
+ return result;
}
},
}); 📝 Committable suggestion
Suggested change
🧰 Tools🪛 Biome (1.9.4)[error] 23-23: Unsafe usage of 'return'. 'return' in 'finally' overwrites the control flow statements inside 'try' and 'catch'. (lint/correctness/noUnsafeFinally) |
||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
["log", "warn", "info"].forEach((method) => { | ||||||||||||||||||||||||||||||||||||||||||||||
nativeConsole[method] = createProxy(method); | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
window.addEventListener("error", (event) => { | ||||||||||||||||||||||||||||||||||||||||||||||
postMessage("error", [event.message]); | ||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||
})(window.console); |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,28 @@ | ||||||||||||||||||||||||||||||||||||||||
export const CUSTOM_WIDGET_LOAD_EVENTS = { | ||||||||||||||||||||||||||||||||||||||||
STARTED: "started", | ||||||||||||||||||||||||||||||||||||||||
DOM_CONTENTED_LOADED: "DOMContentLoaded", | ||||||||||||||||||||||||||||||||||||||||
COMPLETED: "completed", | ||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export const getAppsmithScriptSchema = (model: Record<string, unknown>) => ({ | ||||||||||||||||||||||||||||||||||||||||
appsmith: { | ||||||||||||||||||||||||||||||||||||||||
mode: "", | ||||||||||||||||||||||||||||||||||||||||
model: model, | ||||||||||||||||||||||||||||||||||||||||
ui: { | ||||||||||||||||||||||||||||||||||||||||
width: 1, | ||||||||||||||||||||||||||||||||||||||||
height: 2, | ||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+11
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Avoid hard-coded UI dimensions. The UI dimensions should be configurable or use meaningful defaults. ui: {
- width: 1,
- height: 2,
+ width: DEFAULT_WIDGET_WIDTH,
+ height: DEFAULT_WIDGET_HEIGHT,
},
|
||||||||||||||||||||||||||||||||||||||||
theme: { | ||||||||||||||||||||||||||||||||||||||||
primaryColor: "", | ||||||||||||||||||||||||||||||||||||||||
backgroundColor: "", | ||||||||||||||||||||||||||||||||||||||||
borderRadius: "", | ||||||||||||||||||||||||||||||||||||||||
boxShadow: "", | ||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+15
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add type definitions for theme properties. Theme properties should have proper type definitions for better type safety. +interface ThemeConfig {
+ primaryColor: string;
+ backgroundColor: string;
+ borderRadius: string;
+ boxShadow: string;
+}
+
theme: {
primaryColor: "",
backgroundColor: "",
borderRadius: "",
boxShadow: "",
}, 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
onUiChange: Function, | ||||||||||||||||||||||||||||||||||||||||
onModelChange: Function, | ||||||||||||||||||||||||||||||||||||||||
onThemeChange: Function, | ||||||||||||||||||||||||||||||||||||||||
updateModel: Function, | ||||||||||||||||||||||||||||||||||||||||
triggerEvent: Function, | ||||||||||||||||||||||||||||||||||||||||
onReady: Function, | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+21
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace Function constructor with proper function types. Using the Function constructor is discouraged as it bypasses type checking. Define proper function types instead. - onUiChange: Function,
- onModelChange: Function,
- onThemeChange: Function,
- updateModel: Function,
- triggerEvent: Function,
- onReady: Function,
+ onUiChange: (ui: { width: number; height: number }) => void,
+ onModelChange: (model: Record<string, unknown>) => void,
+ onThemeChange: (theme: ThemeConfig) => void,
+ updateModel: (model: Record<string, unknown>) => void,
+ triggerEvent: (eventName: string, eventData?: unknown) => void,
+ onReady: () => void, 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enhance security of postMessage communication
Using "*" as the targetOrigin in postMessage is a security risk. Consider restricting it to specific origins.
📝 Committable suggestion