-
-
Notifications
You must be signed in to change notification settings - Fork 435
/
Copy pathcloud-share-sketch-dialog.tsx
180 lines (166 loc) · 4.89 KB
/
cloud-share-sketch-dialog.tsx
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import * as React from '@theia/core/shared/react';
import { inject, injectable } from '@theia/core/shared/inversify';
import { Widget } from '@theia/core/shared/@phosphor/widgets';
import { Message } from '@theia/core/shared/@phosphor/messaging';
import { clipboard } from 'electron';
import { ReactWidget, DialogProps } from '@theia/core/lib/browser';
import { AbstractDialog } from '../theia/dialogs/dialogs';
import { CreateApi } from '../create/create-api';
import { nls } from '@theia/core/lib/common';
const RadioButton = (props: {
id: string;
changed: (evt: React.BaseSyntheticEvent) => void;
value: string;
isSelected: boolean;
isDisabled: boolean;
label: string;
}) => {
return (
<p className="RadioButton">
<input
id={props.id}
onChange={props.changed}
value={props.value}
type="radio"
checked={props.isSelected}
disabled={props.isDisabled}
/>
<label htmlFor={props.id}>{props.label}</label>
</p>
);
};
export const ShareSketchComponent = ({
treeNode,
createApi,
domain = 'https://create.arduino.cc',
}: {
treeNode: any;
createApi: CreateApi;
domain?: string;
}): React.ReactElement => {
const [loading, setloading] = React.useState<boolean>(false);
const radioChangeHandler = async (event: React.BaseSyntheticEvent) => {
setloading(true);
const sketch = await createApi.editSketch({
id: treeNode.sketchId,
params: {
is_public: event.target.value === 'private' ? false : true,
},
});
// setPublicVisibility(sketch.is_public);
treeNode.isPublic = sketch.is_public;
setloading(false);
};
const sketchLink = `${domain}/editor/_/${treeNode.sketchId}/preview`;
const embedLink = `<iframe src="${sketchLink}?embed" style="height:510px;width:100%;margin:10px 0" frameborder=0></iframe>`;
return (
<div id="widget-container arduino-sharesketch-dialog">
<p>
{nls.localize(
'arduino/cloud/chooseSketchVisibility',
'Choose visibility of your Sketch:'
)}
</p>
<RadioButton
changed={radioChangeHandler}
id="1"
isSelected={treeNode.isPublic === false}
label={nls.localize(
'arduino/cloud/privateVisibility',
'Private. Only you can view the Sketch.'
)}
value="private"
isDisabled={loading}
/>
<RadioButton
changed={radioChangeHandler}
id="2"
isSelected={treeNode.isPublic === true}
label={nls.localize(
'arduino/cloud/publicVisibility',
'Public. Anyone with the link can view the Sketch.'
)}
value="public"
isDisabled={loading}
/>
{treeNode.isPublic && (
<div>
<p>{nls.localize('arduino/cloud/link', 'Link:')}</p>
<div className="sketch-link">
<input
type="text"
readOnly
value={sketchLink}
className="theia-input"
/>
<button
onClick={() => clipboard.writeText(sketchLink)}
value="copy"
className="theia-button secondary"
>
{nls.localize('vscode/textInputActions/copy', 'Copy')}
</button>
</div>
<p>{nls.localize('arduino/cloud/embed', 'Embed:')}</p>
<div className="sketch-link-embed">
<textarea
readOnly
value={embedLink}
className="theia-input stretch"
/>
</div>
</div>
)}
</div>
);
};
@injectable()
export class ShareSketchWidget extends ReactWidget {
constructor(private treeNode: any, private createApi: CreateApi) {
super();
}
protected render(): React.ReactNode {
return (
<ShareSketchComponent
treeNode={this.treeNode}
createApi={this.createApi}
/>
);
}
}
@injectable()
export class ShareSketchDialogProps extends DialogProps {
readonly node: any;
readonly createApi: CreateApi;
}
@injectable()
export class ShareSketchDialog extends AbstractDialog<void> {
protected widget: ShareSketchWidget;
constructor(
@inject(ShareSketchDialogProps)
protected override readonly props: ShareSketchDialogProps
) {
super({ title: props.title });
this.contentNode.classList.add('arduino-share-sketch-dialog');
this.widget = new ShareSketchWidget(props.node, props.createApi);
}
get value(): void {
return;
}
protected override onAfterAttach(msg: Message): void {
if (this.widget.isAttached) {
Widget.detach(this.widget);
}
Widget.attach(this.widget, this.contentNode);
super.onAfterAttach(msg);
this.update();
}
protected override onUpdateRequest(msg: Message): void {
super.onUpdateRequest(msg);
this.widget.update();
}
protected override onActivateRequest(msg: Message): void {
super.onActivateRequest(msg);
this.widget.activate();
}
}