-
-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathExternal.js
130 lines (117 loc) · 2.64 KB
/
External.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import Adw from "gi://Adw";
import dbus_previewer from "./DBusPreviewer.js";
export default function External({ output, builder, onStop }) {
const stack = builder.get_object("stack_preview");
let dbus_proxy;
dbus_previewer.onStop = onStop;
dbus_previewer.onCssParserError = (error) => {
builder
.get_object("code_view_css")
.handleDiagnostics([getCssDiagnostic(error)]);
};
async function start(language) {
if (language === "rust") {
language = "vala"; // Rust uses the Vala previewer.
}
try {
dbus_proxy = await dbus_previewer.getProxy(language);
} catch (err) {
console.error(err);
}
}
async function open() {
updateColorScheme();
stack.set_visible_child_name("close_window");
try {
await dbus_proxy.OpenWindowAsync(
output.get_allocated_width(),
output.get_allocated_height(),
);
} catch (err) {
console.debug(err);
}
}
async function close() {
try {
await dbus_proxy.CloseWindowAsync();
} catch (err) {
console.debug(err);
return;
}
}
function stop() {
close().then(() => dbus_previewer.stop());
}
async function updateXML({ xml, target_id, original_id }) {
try {
await dbus_proxy.UpdateUiAsync(xml, target_id, original_id || "");
} catch (err) {
console.debug(err);
}
}
async function openInspector() {
try {
await dbus_proxy.EnableInspectorAsync(true);
} catch (err) {
console.debug(err);
}
}
async function closeInspector() {
try {
await dbus_proxy.EnableInspectorAsync(false);
} catch (err) {
console.debug(err);
}
}
const style_manager = Adw.StyleManager.get_default();
function updateColorScheme() {
try {
dbus_proxy.ColorScheme = style_manager.color_scheme;
} catch (err) {
console.debug(err);
}
}
style_manager.connect("notify::color-scheme", updateColorScheme);
return {
start,
stop,
open,
close,
openInspector,
closeInspector,
updateXML,
async updateCSS(css) {
try {
await dbus_proxy.UpdateCssAsync(css);
} catch (err) {
console.debug(err);
}
},
async screenshot({ path }) {
return dbus_proxy.ScreenshotAsync(path);
},
};
}
// Converts a CssParserError to an LSP diagnostic object
function getCssDiagnostic([
message,
start_line,
start_char,
end_line,
end_char,
]) {
return {
message,
range: {
start: {
line: start_line,
character: start_char,
},
end: {
line: end_line,
character: end_char,
},
},
severity: 1,
};
}