Loading...
Searching...
No Matches
LibraryHandler.hpp
1#pragma once
2#include <JS/ConsolePanel.hpp>
3#include <JS/JSProcessModel.hpp>
4#include <Library/LibraryInterface.hpp>
5#include <Library/ProcessesItemModel.hpp>
6
7#include <QFile>
8#include <QFileInfo>
9namespace JS
10{
12 : public QObject
14{
15 SCORE_CONCRETE("6e72e377-efdd-4e3c-9900-922b618e7d70")
16
17public:
18 JS::PanelDelegate* panel{};
19
20 QSet<QString> acceptedFiles() const noexcept override { return {"mjs"}; }
21
22 bool add(const QString& path)
23 {
24 QFile f{path};
25 if(!f.open(QIODevice::ReadOnly))
26 return false;
27
28 if(!panel)
29 panel = &score::GUIAppContext().panel<JS::PanelDelegate>();
30
31 panel->importModule(path);
32 return true;
33 }
34
35 void addPath(std::string_view path) override
36 {
37 if(path.find("companion-bundled-modules") != std::string_view::npos)
38 return;
39 if(path.find("node_modules") != std::string_view::npos)
40 return;
41
42 add(QString::fromUtf8(path.data(), path.length()));
43 }
44
45 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
46 {
47 return add(path);
48 }
49};
50
52 : public QObject
54{
55 SCORE_CONCRETE("21f405da-a249-4e39-b405-9173aff11b26")
56
57 QSet<QString> acceptedFiles() const noexcept override { return {"js"}; }
58
59 bool onDoubleClick(const QString& path, const score::DocumentContext& ctx) override
60 {
61 QFile f{path};
62 if(!f.open(QIODevice::ReadOnly))
63 return false;
64
65 auto& p = ctx.app.panel<JS::PanelDelegate>();
66 if(QFileInfo{f}.suffix() == "mjs")
67 {
68 p.importModule(path);
69 }
70 else
71 {
72 auto data = f.readAll();
73 p.evaluate(data);
74 }
75 return true;
76 }
77};
78
79class LibraryHandler final
80 : public QObject
82{
83 SCORE_CONCRETE("5231ea8b-da66-4c6f-9e34-d9a79cbc494a")
84
85 QSet<QString> acceptedFiles() const noexcept override { return {"qml"}; }
86
87 static inline const QRegularExpression scoreImport{"import Score [0-9].[0-9]"};
88
89 Library::Subcategories categories;
90
92 override
93 {
94 // TODO relaunch whenever library path changes...
96 QModelIndex node = model.find(key);
97 if(node == QModelIndex{})
98 return;
99
100 categories.init(node, ctx);
101 }
102
103 void addPath(std::string_view path) override
104 {
105 QFileInfo file{QString::fromUtf8(path.data(), path.length())};
107 pdata.prettyName = file.completeBaseName();
109 pdata.customData = [&] {
110 QFile f(file.absoluteFilePath());
111 f.open(QIODevice::ReadOnly);
112 return f.readAll().trimmed();
113 }();
114
115 {
116 auto matches = scoreImport.match(pdata.customData);
117 if(matches.hasMatch())
118 {
119 categories.add(file, std::move(pdata));
120 }
121 }
122 }
123};
124
125}
Definition LibraryHandler.hpp:54
Definition LibraryHandler.hpp:82
Definition LibraryHandler.hpp:14
Definition ConsolePanel.hpp:37
Definition LibraryInterface.hpp:22
Definition ProcessesItemModel.hpp:43
Definition ProcessesItemModel.hpp:32
Definition ProcessesItemModel.hpp:84
Static metadata implementation.
Definition lib/score/tools/Metadata.hpp:36
Definition ObjectMatches.hpp:6
Definition DocumentContext.hpp:18
Specializes ApplicationContext with the QMainWindow.
Definition GUIApplicationContext.hpp:15
T & panel() const
Access to a specific PanelDelegate.
Definition GUIApplicationContext.hpp:36