Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes needed for VSCode webview extension #186

Merged
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2360563
Fix accessing localStorage crashing VSCode webview
TranquilMarmot May 23, 2020
68ce41b
Add "getPublicPath" to Utils that will use a "window.getPublicVsCodeW…
TranquilMarmot May 23, 2020
3242f84
Don't create start node if opening a file with VS Code extension
TranquilMarmot May 24, 2020
6e9770f
Send save event to VSCode extension
TranquilMarmot May 24, 2020
1e74549
Send message to extension when settings are changes
TranquilMarmot May 25, 2020
d7463aa
Update some comments
TranquilMarmot May 25, 2020
dd1fd9f
More specific comment for Utils.getPublicPath
TranquilMarmot May 25, 2020
4012451
Lint
TranquilMarmot May 25, 2020
4564968
Fix #app positioning
TranquilMarmot May 25, 2020
e399311
Fix #app positioning
TranquilMarmot May 25, 2020
d97d13e
Tell VSCode extension when document has changed
TranquilMarmot May 25, 2020
585b6a5
Fully rely on VSCode settings
TranquilMarmot May 25, 2020
4e5b6b3
Fix references to updateVsCodeExtensionDocument
TranquilMarmot May 25, 2020
2b9ff93
Don't try and save if we're editing in VSCode
TranquilMarmot May 25, 2020
4ad0be1
Lint
TranquilMarmot May 25, 2020
c588396
Don't attempt to send message to extension unless we're editing a file
TranquilMarmot May 25, 2020
17cb77f
openingVsCodeFile -> editingVsCodeFile
TranquilMarmot May 26, 2020
b80f150
Mark document as dirty when changing node colors
TranquilMarmot May 26, 2020
856d55f
Fix markup language setting not being applied
TranquilMarmot May 28, 2020
404aa85
FIx crash when node styles are undefined
TranquilMarmot May 28, 2020
7ef7e4c
Merge branch 'tranquilmarmot/vscode-yarn-spinner' of https://github.c…
TranquilMarmot May 28, 2020
f676bf1
Clean up if/else statement
TranquilMarmot May 28, 2020
0488a35
Merge branch 'master' of github.com:YarnSpinnerTool/YarnEditor into t…
TranquilMarmot May 29, 2020
9e5d250
Ability to open nodes in a text editor
TranquilMarmot May 29, 2020
400b212
Clean up extension message listener
TranquilMarmot May 30, 2020
4205b19
Add alwaysOpenNodesInVisualStudioCodeEditor option
TranquilMarmot May 30, 2020
265c9b8
Lint...
TranquilMarmot May 30, 2020
7ec91ae
Fix "Edit in Visual Studio Code Text Editor" showing up in web app
TranquilMarmot May 30, 2020
8d99f8f
Lint
TranquilMarmot May 30, 2020
b398e6e
Only update VSCode document when dragging ends
TranquilMarmot May 30, 2020
64156b9
Merge branch 'master' of github.com:YarnSpinnerTool/YarnEditor into t…
TranquilMarmot May 31, 2020
8d693c0
Merge branch 'master' of github.com:YarnSpinnerTool/YarnEditor into t…
TranquilMarmot Jun 6, 2020
84f9039
updateVsCodeExtensionDocument -> setYarnDocumentIsDirty
TranquilMarmot Jun 10, 2020
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
</span>

<button class="bbcode-button" onclick="app.insertEmoji()" title="Emoji">😉</button>

<!-- ko if:vsCodeApi != null -->
<button class="bbcode-button" id="edit-node-in-vscode" onclick="app.editNodeInVisualStudioCodeEditor(app.editing())">🛠️ Edit in Visual Studio Code Text Editor</button>
TranquilMarmot marked this conversation as resolved.
Show resolved Hide resolved
<!-- /ko -->

<div class="add-link" style="position: absolute;">
<div class="menu">
Expand Down
72 changes: 70 additions & 2 deletions src/js/classes/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export var App = function(name, version) {
this.setTheme = function(name, e) {
let themeName = e ? e.target.value : name;
const theme = document.getElementById('theme-stylesheet');
theme.href = `./public/themes/${themeName}.css`;
theme.href = Utils.getPublicPath(`themes/${themeName}.css`);
};

this.setLanguage = function(language, e) {
Expand Down Expand Up @@ -157,7 +157,12 @@ export var App = function(name, version) {
$('#app').show();
ko.applyBindings(self, $('#app')[0]);

self.newNode().title('Start');
// this is set in the VSCode extension YarnEditorPanel
// this is true when we're opening a file in the VSCode extension;
// adding that start node here was causing issues with arrows (because of race conditions)
if (!window.editingVsCodeFile) {
self.newNode().title('Start');
}

self.settings.apply();

Expand Down Expand Up @@ -358,6 +363,27 @@ export var App = function(name, version) {
);
}
};

// this is how the VSCode extension sends its messages back to the app
window.addEventListener('message', (event) => {
const message = event.data;

// sent whenever the temporary file that's open gets changed
if (message.type === 'UpdateNode') {
self.nodes().forEach(node => {
if (
node
.title()
.trim()
.toLowerCase() === message.payload.nodeName.trim().toLowerCase()
) {
node.body(message.payload.nodeText);
self.updateVsCodeExtensionDocument();
}
});
}
});

// Callback for embedding in other webapps
var event = new CustomEvent('yarnReady');
event.document = document;
Expand Down Expand Up @@ -418,6 +444,20 @@ export var App = function(name, version) {
}
};

// This should be called whenever we want to mark the document that the VSCcode extension has opened as changed.
// If we're not in the extension working on an open file, this is a no-op.
// This should be called after every action that will result in a changed document.
this.updateVsCodeExtensionDocument = function() {
Copy link
Contributor Author

@TranquilMarmot TranquilMarmot May 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extension relies on this being called to update its internal document and mark the document as "dirty" so that it can be saved.

I think I got all the places that this needs to be called...

  • Node added/removed
  • Node dragged
  • Node text editor closed
  • Node color changed

Copy link
Owner

@blurymind blurymind May 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can benefit from this documentIsDirty in the electron version to add an * to the window title after a file has been edited.

Can we perhaps refactor this so yarn in general knows when a document is dirty (when running in electron or vscode).

If not, thats fine :)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually can you just rename this function from
this.updateVsCodeExtensionDocument
to
this.setYarnDocumentIsDirty

later on we can use it to handle the electron case

@daviddq do you think it would be a good idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

if (window.vsCodeApi && window.editingVsCodeFile) {
window.vsCodeApi.postMessage({
type: 'DocumentEdit',

// we just send the whole doc here every time...
payload: data.getSaveData(data.editingType())
});
}
};

this.recordNodeAction = function(action, node) {
//we can't go forward in 'time' when
//new actions have been made
Expand All @@ -443,6 +483,8 @@ export var App = function(name, version) {
}

self.nodeHistory.push(historyItem);

self.updateVsCodeExtensionDocument();
};

this.historyDirection = function(direction) {
Expand Down Expand Up @@ -475,6 +517,7 @@ export var App = function(name, version) {
}

self.nodeFuture.push(historyItem);
self.updateVsCodeExtensionDocument();
} //redo undone actions
else {
if (action == 'created') {
Expand All @@ -484,6 +527,7 @@ export var App = function(name, version) {
}

self.nodeHistory.push(historyItem);
self.updateVsCodeExtensionDocument();
}
};

Expand Down Expand Up @@ -531,6 +575,7 @@ export var App = function(name, version) {
self.workspace.removeNodesFromSelection(nodes[i]);
nodes[i].remove();
}
self.updateVsCodeExtensionDocument();
};

this.cloneNodeArray = function(nodeArray) {
Expand Down Expand Up @@ -601,6 +646,27 @@ export var App = function(name, version) {
});
};

// called by the "Edit in Visual Studio Code Text Editor" button
// this sends a message to the extension telling it to open the node in a text editor
this.editNodeInVisualStudioCodeEditor = function(node) {
if (window.vsCodeApi) {
// updating the document is actually a trick to force VSCode to think the open document is
// dirty so that if it's not "pinned" it won't close when the editor swaps
self.updateVsCodeExtensionDocument();

// tell VSCode extension to open our node in a new editor
window.vsCodeApi.postMessage({
type: "OpenNode",
payload: {
nodeName: node.title().trim().toLowerCase(),
nodeText: self.trimBodyLinks(node.body().trim())
}
});

return;
}
}

this.editNode = function(node) {
if (!node.active()) {
return;
Expand Down Expand Up @@ -965,6 +1031,8 @@ export var App = function(name, version) {
self.editing(null);
});
}

self.updateVsCodeExtensionDocument();
}
};

Expand Down
6 changes: 6 additions & 0 deletions src/js/classes/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,12 @@ export var data = {
},

save: function() {
if (window.editingVsCodeFile) {
// if we're editing a file in the VSCode extension, it handles
// saving the file on its end so we do nothing here
return;
}

if (self.editingPath())
self.trySaveCurrent();
else
Expand Down
25 changes: 25 additions & 0 deletions src/js/classes/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,29 @@ export let Node = function(options = {}) {

this.x = function(inX) {
if (inX != undefined) $(self.element).css({ x: Math.floor(inX) });

// if we don't have a style here, it means this node is in the
// process of being created and self.element doesn't exist yet
if (!self.style) {
return;
}

// m41 here corresponds to the fourth row and first column of the matrix transform
// this is the X value of the transform
return Math.floor(new WebKitCSSMatrix(self.style.webkitTransform).m41);
};

this.y = function(inY) {
if (inY != undefined) $(self.element).css({ y: Math.floor(inY) });

// if we don't have a style here, it means this node is in the
// process of being created and self.element doesn't exist yet
if (!self.style) {
return;
}

// m42 here corresponds to the fourth row and second column of the matrix transform
// this is the X value of the transform
return Math.floor(new WebKitCSSMatrix(self.style.webkitTransform).m42);
};

Expand All @@ -160,6 +178,8 @@ export let Node = function(options = {}) {
if (app.input.isShiftDown) app.matchConnectedColorID(self);

if (self.selected) app.setSelectedColors(self);

app.updateVsCodeExtensionDocument();
};

this.cycleColorUp = function() {
Expand All @@ -171,6 +191,8 @@ export let Node = function(options = {}) {
if (app.input.isShiftDown) app.matchConnectedColorID(self);

if (self.selected) app.setSelectedColors(self);

app.updateVsCodeExtensionDocument();
};

this.doCycleColorDown = function() {
Expand Down Expand Up @@ -241,6 +263,9 @@ export let Node = function(options = {}) {
}

app.workspace.updateArrows();

// this will tell the VSCode extension that we've moved the node
app.updateVsCodeExtensionDocument();
}
});

Expand Down
17 changes: 16 additions & 1 deletion src/js/classes/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// Get the mechanism to use for storage.
const getStorage = function() {
// if `window.vsCodeApi` exists, we're in the context of the VSCode extension
// which handles all of the settings internally, so we don't need to do anything here
if (window.vsCodeApi) {
return {
getItem: () => {},
setItem: () => {}
};
} else {
return window.localStorage;
}
};

export const Settings = function(app) {
const self = this;
const storage = window.localStorage;
const storage = getStorage();

ko.extenders.persist = function (target, option) {
target.subscribe(function (newValue) {
Expand All @@ -16,6 +30,7 @@ export const Settings = function(app) {
app.setTheme(self.theme());
app.setLanguage(self.language());
app.toggleNightMode();
app.setMarkupLanguage(self.markupLanguage());
app.workspace.setThrottle(self.redrawThrottle());
};

Expand Down
13 changes: 13 additions & 0 deletions src/js/classes/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,18 @@ export var Utils = {
Date.now ||
function() {
return new Date().getTime();
},

// If we're in the context of the VSCode extension webview, we have to use a function
// that it puts on window to get the path to public assets since it requires that
// they be loaded with fully-qualified, special scheme.
//
// The "path" parameter here should NOT have a leading slash.
getPublicPath: function(path) {
if (window.getPublicVsCodeWebviewUri) {
return window.getPublicVsCodeWebviewUri(path);
} else {
return `public${path ? `/${path}` : ''}`;
}
}
};
4 changes: 3 additions & 1 deletion src/js/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import '../scss/index.scss';

import { Utils } from './classes/utils';

import * as OfflinePluginRuntime from 'offline-plugin/runtime';
OfflinePluginRuntime.install();

Expand All @@ -12,7 +14,7 @@ import 'jquery-mousewheel';

import ace from 'ace-builds/src-noconflict/ace';
window.ace = ace;
ace.config.set('basePath', 'public'); //needed to import yarn mode
ace.config.set('basePath', Utils.getPublicPath()); //needed to import yarn mode
window.define = ace.define;

import 'ace-builds/src-min-noconflict/ext-language_tools';
Expand Down
10 changes: 6 additions & 4 deletions src/js/libs/spellcheck_ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ var nspell = require('nspell');
// You should configure these classes.
var editor = 'editor'; // This should be the id of your editor element.

var dicPath = 'public/dictionaries/en/index.dic';
var affPath = 'public/dictionaries/en/index.aff';
var utils = require('../classes/utils');

var dicPath = utils.Utils.getPublicPath('dictionaries/en/index.dic');
var affPath = utils.Utils.getPublicPath('dictionaries/en/index.aff');
// var dicPath =
// "https://raw.githubusercontent.com/elastic/hunspell/master/dicts/en_US/en_US.dic";
// var affPath =
Expand All @@ -28,8 +30,8 @@ var dictionary = null;

function load_dictionary(dicLanguage) {
console.info(`Loading ${dicLanguage} hunspell dictionary locally`);
dicPath = `public/dictionaries/${dicLanguage}/index.dic`;
affPath = `public/dictionaries/${dicLanguage}/index.aff`;
dicPath = utils.Utils.getPublicPath(`dictionaries/${dicLanguage}/index.dic`);
affPath = utils.Utils.getPublicPath(`dictionaries/${dicLanguage}/index.aff`);

$.get(dicPath, function(data) {
dicData = data;
Expand Down
7 changes: 7 additions & 0 deletions src/scss/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ input[type='checkbox'] {

#app {
position: absolute;
top: 0px;
left: 0px;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason, in the VSCode webview, #app was a shifted few pixels to the right which was causing issues like arrows not rendering in the right spot.

Adding this gets it to the right spot, and doesn't seem to have any effect on the web app.

width: 100%;
height: 100%;
cursor: default;
Expand Down Expand Up @@ -552,6 +554,11 @@ input[type='checkbox'] {
background-color: rgb(255, 255, 172);
}

#edit-node-in-vscode {
float: right;
font-size: 125%;
}

/* SPECIAL CHECKBOX START */
.styled-checkbox input {
-webkit-transition-duration: 0.4s; /* Safari */
Expand Down