Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 2 additions & 84 deletions extensions/BetterShare/index.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,4 @@
(function () {
function copyToClipboard(txt) {
navigator.clipboard.writeText(txt);
}

function ShareMorph(url) {
this.url = url;
this.init();
}
ShareMorph.prototype = new DialogBoxMorph();
ShareMorph.prototype.constructor = ShareMorph;
ShareMorph.uber = DialogBoxMorph.prototype;

ShareMorph.prototype.init = function() {
ShareMorph.uber.init.call(this);

this.labelString = 'BetterShare';
this.createLabel();

const [width, height] = [300, 300];
this.bounds.setWidth(width);
this.bounds.setHeight(height);

this.add(this.linkLabel = new StringMorph('Share Link:'));
this.add(this.linkField = new StringFieldMorph(this.url, width - 100));
this.add(this.copyButton = new PushButtonMorph(null, () => copyToClipboard(this.url), 'Copy'));
this.add(this.closeButton = new PushButtonMorph(null, () => this.destroy(), 'Close'));

const [qrwidth, qrheight] = [150, 150];
this.add(this.qrcode = new Morph());
this.qrcode.setWidth(qrwidth);
this.qrcode.setHeight(qrheight);
this.qrcode.texture = `https://api.qrserver.com/v1/create-qr-code/?size=${qrwidth}x${qrheight}&data=${encodeURIComponent(this.url)}`;

this.fixLayout();
this.rerender();
};
ShareMorph.prototype.fixLayout = function() {
ShareMorph.uber.fixLayout.call(this);

if (this.linkLabel) {
this.linkLabel.setCenter(this.center());
this.linkLabel.setTop(35);
}
if (this.linkField) {
this.linkField.setTop(60);
this.linkField.setLeft(25);
}
if (this.copyButton) {
this.copyButton.setTop(56);
this.copyButton.setRight(this.right() - 25);
}
if (this.qrcode) {
this.qrcode.setCenter(this.center());
this.qrcode.setTop(90);
}
if (this.closeButton) {
this.closeButton.setCenter(this.center());
this.closeButton.setBottom(this.bottom() - 20);
}
};

class BetterShare extends Extension {
constructor(ide) {
super('BetterShare');
Expand All @@ -68,29 +7,8 @@

getMenu() {
return {
'Share Project': async () => {
const projectId = this.ide?.cloud?.projectId;
const res = await this.ide.cloud.publishProject(projectId);

// TODO: this feels wrong in terms of the cloud response - if ever fixed in cloud, update here
if (res === 'Public') {
this.ide.showMessage('Project must first be saved to the cloud!');
return;
}

const urlParams = new URLSearchParams(window.location.search);

const username = urlParams.get('Username') || this.ide?.cloud?.username;
const projName = urlParams.get('ProjectName') || this.ide?.room?.name;
const roleName = urlParams.get('Role') || this.ide?.projectName;

if (username && projName && roleName) {
const shareLink = `${location.origin}/?action=present&editMode&noRun&Username=${encodeURIComponent(username)}&ProjectName=${encodeURIComponent(projName)}&Role=${encodeURIComponent(roleName)}`;
new ShareMorph(shareLink).popUp(world);
} else {
this.ide.showMessage('Failed to get shared project info');
}
}
'Share Project': this.ide.share,
'Unshare Project': this.ide.unshare,
};
}
}
Expand Down