Skip to content

Commit 80145af

Browse files
committed
console api: add a function to save the current score as json
1 parent 7d2bf0e commit 80145af

4 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/plugins/score-plugin-js/JS/Qml/EditContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ void EditJsContext::redo()
4444
QObject* EditJsContext::find(QString p)
4545
{
4646
auto doc = document();
47+
if(!doc)
48+
return nullptr;
49+
4750
const auto meta = doc->findChildren<score::ModelMetadata*>();
4851
for(auto m : meta)
4952
{

src/plugins/score-plugin-js/JS/Qml/EditContext.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ class SCORE_PLUGIN_JS_EXPORT EditJsContext : public QObject
287287
QVariant availableProtocols() const noexcept;
288288
W_SLOT(availableProtocols)
289289

290+
QByteArray serializeAsJson() noexcept;
291+
W_SLOT(serializeAsJson)
290292
private:
291293
void submit(Macro& m, score::Command* c);
292294
};

src/plugins/score-plugin-js/JS/Qml/EditContext.introspection.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ QVariant EditJsContext::availableProtocols() const noexcept
4444
}
4545
return v;
4646
}
47+
48+
QByteArray EditJsContext::serializeAsJson() noexcept
49+
{
50+
auto doc = (score::Document*)document();
51+
if(!doc)
52+
return {};
53+
JSONReader w;
54+
w.buffer.Reserve(1024 * 1024 * 16);
55+
56+
doc->saveAsJson(w);
57+
return w.toByteArray();
58+
}
4759
}

src/plugins/score-plugin-js/JS/Qml/Utils.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <JS/Qml/Utils.hpp>
22

3+
#include <score/tools/File.hpp>
34
#include <score/tools/ThreadPool.hpp>
45

56
#include <ossia/detail/algorithms.hpp>
@@ -17,6 +18,9 @@ namespace JS
1718

1819
QByteArray JsUtils::readFile(QString path)
1920
{
21+
if(auto doc = score::AppContext().currentDocument())
22+
path = score::locateFilePath(path, *doc);
23+
2024
QFile f(path);
2125
if(f.open(QIODevice::ReadOnly))
2226
return f.readAll();

0 commit comments

Comments
 (0)