Exposure document.canvas to javascript api

master
Jeremy Hu 2019-07-27 10:37:07 +09:30
parent 59a59e4fb3
commit a833232d3c
2 changed files with 64 additions and 34 deletions

View File

@ -5,6 +5,7 @@
#include "scriptrunner.h" #include "scriptrunner.h"
#include "util.h" #include "util.h"
JSClassID ScriptRunner::js_canvasClassId = 0;
JSClassID ScriptRunner::js_partClassId = 0; JSClassID ScriptRunner::js_partClassId = 0;
JSClassID ScriptRunner::js_componentClassId = 0; JSClassID ScriptRunner::js_componentClassId = 0;
JSClassID ScriptRunner::js_nodeClassId = 0; JSClassID ScriptRunner::js_nodeClassId = 0;
@ -51,7 +52,14 @@ static ScriptRunner::DocumentElement *GetElementFromArg(JSValueConst arg)
ScriptRunner::DocumentComponent *component = (ScriptRunner::DocumentComponent *)JS_GetOpaque(arg, ScriptRunner::DocumentComponent *component = (ScriptRunner::DocumentComponent *)JS_GetOpaque(arg,
ScriptRunner::js_componentClassId); ScriptRunner::js_componentClassId);
if (nullptr != component) { if (nullptr != component) {
element = (ScriptRunner::DocumentComponent *)component; element = (ScriptRunner::DocumentElement *)component;
}
}
if (nullptr == element) {
ScriptRunner::DocumentCanvas *canvas = (ScriptRunner::DocumentCanvas *)JS_GetOpaque(arg,
ScriptRunner::js_canvasClassId);
if (nullptr != canvas) {
element = (ScriptRunner::DocumentElement *)canvas;
} }
} }
return element; return element;
@ -520,6 +528,7 @@ void ScriptRunner::run()
countTimeConsumed.start(); countTimeConsumed.start();
// Warning: Not thread safe, but we have only one script instance running, so it doesn't matter // Warning: Not thread safe, but we have only one script instance running, so it doesn't matter
js_canvasClassId = JS_NewClassID(&js_canvasClassId);
js_partClassId = JS_NewClassID(&js_partClassId); js_partClassId = JS_NewClassID(&js_partClassId);
js_componentClassId = JS_NewClassID(&js_componentClassId); js_componentClassId = JS_NewClassID(&js_componentClassId);
js_nodeClassId = JS_NewClassID(&js_nodeClassId); js_nodeClassId = JS_NewClassID(&js_nodeClassId);
@ -542,39 +551,47 @@ void ScriptRunner::run()
JSValue globalObject = JS_GetGlobalObject(context); JSValue globalObject = JS_GetGlobalObject(context);
JSValue document = JS_NewObject(context); JSValue document = JS_NewObject(context);
JS_SetPropertyStr(context,
document, "createComponent", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createComponent, "createComponent", 1)); document, "createComponent",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createComponent, "createComponent", 1));
document, "createPart", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createPart, "createPart", 1)); document, "createPart",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createPart, "createPart", 1));
document, "createNode", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createNode, "createNode", 1)); document, "createNode",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createNode, "createNode", 1));
document, "createFloatInput", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createFloatInput, "createFloatInput", 4)); document, "createFloatInput",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createFloatInput, "createFloatInput", 4));
document, "createIntInput", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createIntInput, "createIntInput", 4)); document, "createIntInput",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createIntInput, "createIntInput", 4));
document, "createColorInput", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createColorInput, "createColorInput", 2)); document, "createColorInput",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createColorInput, "createColorInput", 2));
document, "createCheckInput", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createCheckInput, "createCheckInput", 2)); document, "createCheckInput",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createCheckInput, "createCheckInput", 2));
document, "createSelectInput", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_createSelectInput, "createSelectInput", 3)); document, "createSelectInput",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_createSelectInput, "createSelectInput", 3));
document, "connect", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_connect, "connect", 2)); document, "connect",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_connect, "connect", 2));
document, "setAttribute", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_setAttribute, "setAttribute", 3)); document, "setAttribute",
JS_SetPropertyStr(context, JS_NewCFunction(context, js_setAttribute, "setAttribute", 3));
document, "attribute", JS_SetPropertyStr(context,
JS_NewCFunction(context, js_attribute, "attribute", 2)); document, "attribute",
JS_NewCFunction(context, js_attribute, "attribute", 2));
JSValue canvas = JS_NewObjectClass(context, ScriptRunner::js_canvasClassId);
JS_SetOpaque(canvas, &m_canvas);
JS_SetPropertyStr(context,
document, "canvas",
canvas);
JS_SetPropertyStr(context, globalObject, "document", document); JS_SetPropertyStr(context, globalObject, "document", document);
JSValue console = JS_NewObject(context); JSValue console = JS_NewObject(context);
@ -649,6 +666,8 @@ void ScriptRunner::generateSnapshot()
QStringList rootChildren; QStringList rootChildren;
m_resultSnapshot->canvas = m_canvas.attributes;
for (const auto &it: m_components) { for (const auto &it: m_components) {
QString idString = it->id; QString idString = it->id;
pointerToIdMap[it] = idString; pointerToIdMap[it] = idString;

View File

@ -16,6 +16,7 @@ public:
enum class DocumentElementType enum class DocumentElementType
{ {
Unknown = 0, Unknown = 0,
Canvas,
Component, Component,
Part, Part,
Node Node
@ -56,6 +57,14 @@ public:
DocumentPart *part = nullptr; DocumentPart *part = nullptr;
}; };
struct DocumentCanvas : DocumentElement
{
DocumentCanvas()
{
type = DocumentElementType::Canvas;
}
};
ScriptRunner(); ScriptRunner();
~ScriptRunner(); ~ScriptRunner();
void run(); void run();
@ -93,8 +102,10 @@ private:
std::vector<std::pair<DocumentNode *, DocumentNode *>> m_edges; std::vector<std::pair<DocumentNode *, DocumentNode *>> m_edges;
QString m_scriptError; QString m_scriptError;
QString m_consoleLog; QString m_consoleLog;
DocumentCanvas m_canvas;
void generateSnapshot(); void generateSnapshot();
public: public:
static JSClassID js_canvasClassId;
static JSClassID js_partClassId; static JSClassID js_partClassId;
static JSClassID js_componentClassId; static JSClassID js_componentClassId;
static JSClassID js_nodeClassId; static JSClassID js_nodeClassId;