Missed some files

This commit is contained in:
Francois Laberge 2021-07-23 08:25:05 -04:00
parent 75b548ced0
commit 1beadb5e34
3 changed files with 186 additions and 160 deletions

View file

@ -1,8 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TldrawEditorProvider = void 0;
const vscode = require("vscode");
const nonce_1 = require("./nonce");
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
exports.TldrawEditorProvider = void 0
const vscode = require('vscode')
const nonce_1 = require('./nonce')
/**
* Provider for .tldr editor.
*
@ -17,21 +17,33 @@ const nonce_1 = require("./nonce");
*/
class TldrawEditorProvider {
constructor(context) {
this.context = context;
this.context = context
}
static register(context) {
vscode.commands.registerCommand('tldraw.tldr.new', () => {
vscode.window.showInformationMessage('Created a new .tldr file');
const workspaceFolders = vscode.workspace.workspaceFolders;
vscode.window.showInformationMessage('Created a new .tldr file')
const workspaceFolders = vscode.workspace.workspaceFolders
if (!workspaceFolders) {
vscode.window.showErrorMessage('Creating new Tldraw Editor files currently requires opening a workspace');
return;
vscode.window.showErrorMessage(
'Creating new Tldraw Editor files currently requires opening a workspace'
)
return
}
const uri = vscode.Uri.joinPath(workspaceFolders[0].uri, `new-${TldrawEditorProvider.newTldrawFileId++}.tldr`).with({ scheme: 'untitled' });
vscode.commands.executeCommand('vscode.openWith', uri, TldrawEditorProvider.viewType);
});
const provider = new TldrawEditorProvider(context);
const providerRegistration = vscode.window.registerCustomEditorProvider(TldrawEditorProvider.viewType, provider, {
const uri = vscode.Uri.joinPath(
workspaceFolders[0].uri,
`new-${TldrawEditorProvider.newTldrawFileId++}.tldr`
).with({ scheme: 'untitled' })
vscode.commands.executeCommand(
'vscode.openWith',
uri,
TldrawEditorProvider.viewType
)
})
const provider = new TldrawEditorProvider(context)
const providerRegistration = vscode.window.registerCustomEditorProvider(
TldrawEditorProvider.viewType,
provider,
{
// For this demo extension, we enable `retainContextWhenHidden` which keeps the
// webview alive even when it is not visible. You should avoid using this setting
// unless is absolutely required as it does have memory overhead.
@ -39,8 +51,9 @@ class TldrawEditorProvider {
retainContextWhenHidden: true,
},
supportsMultipleEditorsPerDocument: false,
});
return providerRegistration;
}
)
return providerRegistration
}
/**
* Called when our custom editor is opened.
@ -48,18 +61,18 @@ class TldrawEditorProvider {
*
*/
async resolveCustomTextEditor(document, webviewPanel, _token) {
this.document = document;
this.document = document
// Setup initial content for the webview
webviewPanel.webview.options = {
enableScripts: true,
retainContextWhenHidden: true,
};
webviewPanel.webview.html = this.getHtmlForWebview(webviewPanel.webview);
}
webviewPanel.webview.html = this.getHtmlForWebview(webviewPanel.webview)
function updateWebview() {
webviewPanel.webview.postMessage({
type: 'load',
text: document.getText(),
});
})
}
// Hook up event handlers so that we can synchronize the webview with the text document.
//
@ -68,63 +81,68 @@ class TldrawEditorProvider {
//
// Remember that a single text document can also be shared between multiple custom
// editors (this happens for example when you split a custom editor)
let firstLoad = true;
const changeDocumentSubscription = vscode.workspace.onDidChangeTextDocument((e) => {
let firstLoad = true
const changeDocumentSubscription = vscode.workspace.onDidChangeTextDocument(
(e) => {
if (e.document.uri.toString() === document.uri.toString()) {
if (firstLoad) {
console.log(`First load:${firstLoad}`);
updateWebview();
firstLoad = false;
}
else {
console.log("don't load");
console.log(`First load:${firstLoad}`)
updateWebview()
firstLoad = false
} else {
console.log("don't load")
}
}
});
}
)
// Make sure we get rid of the listener when our editor is closed.
webviewPanel.onDidDispose(() => {
changeDocumentSubscription.dispose();
});
changeDocumentSubscription.dispose()
})
// Receive message from the webview.
webviewPanel.webview.onDidReceiveMessage((e) => {
switch (e.type) {
case 'update':
console.log(`"update" extension <-`);
console.log(`"update" extension <-`)
//console.log(`Is it different? ${}`)
//vscode.window.showInformationMessage('Upated .tdlr file')
this.updateTextDocument(document, JSON.parse(e.text));
break;
this.updateTextDocument(document, JSON.parse(e.text))
break
case 'save':
console.log(`"save" extension <-`);
console.log(`"save" extension <-`)
if (this.document !== undefined) {
this.document.save();
this.document.save()
// const writeData = Buffer.from(e.text, 'utf8')
// // I believe saving will automatically synchronize the in memory document
// // so we don't need to call updateTextDocument here.
// vscode.workspace.fs.writeFile(this.document?.uri, writeData)
//vscode.window.showInformationMessage('Saved .tdlr file')
}
break;
break
}
});
updateWebview();
})
updateWebview()
}
/**
* Get the static html used for the editor webviews.
*/
getHtmlForWebview(webview) {
// Local path to script and css for the webview
const scriptUri = webview.asWebviewUri(vscode.Uri.joinPath(this.context.extensionUri, 'webview', 'editor.js'));
const scriptUri = webview.asWebviewUri(
vscode.Uri.joinPath(this.context.extensionUri, 'webview', 'editor.js')
)
// const styleResetUri = webview.asWebviewUri(
// vscode.Uri.joinPath(this.context.extensionUri, 'media', 'reset.css')
// )
// const styleVSCodeUri = webview.asWebviewUri(
// vscode.Uri.joinPath(this.context.extensionUri, 'media', 'vscode.css')
// )
const styleMainUri = webview.asWebviewUri(vscode.Uri.joinPath(this.context.extensionUri, 'webview', 'main.css'));
console.log('getHtmlForWebview');
const styleMainUri = webview.asWebviewUri(
vscode.Uri.joinPath(this.context.extensionUri, 'webview', 'main.css')
)
console.log('getHtmlForWebview')
// Use a nonce to whitelist which scripts can be run
const nonce = nonce_1.getNonce();
const nonce = nonce_1.getNonce()
return /* html */ `
<!DOCTYPE html>
<html lang="en">
@ -147,35 +165,40 @@ class TldrawEditorProvider {
<div id="container"></div>
<script nonce="${nonce}" src="${scriptUri}"></script>
</body>
</html>`;
</html>`
}
/**
* Try to get a current document as json text.
*/
getDocumentAsJson(document) {
const text = document.getText();
const text = document.getText()
if (text.trim().length === 0) {
return {};
return {}
}
try {
return JSON.parse(text);
}
catch {
throw new Error('Could not get document as json. Content is not valid json!!!');
return JSON.parse(text)
} catch {
throw new Error(
'Could not get document as json. Content is not valid json!!!'
)
}
}
/**
* Write out the json to a given document.
*/
updateTextDocument(document, json) {
const edit = new vscode.WorkspaceEdit();
const edit = new vscode.WorkspaceEdit()
// Just replace the entire document every time for this example extension.
// A more complete extension should compute minimal edits instead.
edit.replace(document.uri, new vscode.Range(0, 0, document.lineCount, 0), JSON.stringify(json, null, 2));
return vscode.workspace.applyEdit(edit);
edit.replace(
document.uri,
new vscode.Range(0, 0, document.lineCount, 0),
JSON.stringify(json, null, 2)
)
return vscode.workspace.applyEdit(edit)
}
}
exports.TldrawEditorProvider = TldrawEditorProvider;
TldrawEditorProvider.newTldrawFileId = 1;
TldrawEditorProvider.viewType = 'tldraw.tldr';
exports.TldrawEditorProvider = TldrawEditorProvider
TldrawEditorProvider.newTldrawFileId = 1
TldrawEditorProvider.viewType = 'tldraw.tldr'
//# sourceMappingURL=editor-provider.js.map

View file

@ -1,10 +1,12 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.activate = void 0;
const editor_provider_1 = require("./editor-provider");
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
exports.activate = void 0
const editor_provider_1 = require('./editor-provider')
function activate(context) {
// Register our custom editor provider
context.subscriptions.push(editor_provider_1.TldrawEditorProvider.register(context));
context.subscriptions.push(
editor_provider_1.TldrawEditorProvider.register(context)
)
}
exports.activate = activate;
exports.activate = activate
//# sourceMappingURL=extension.js.map

View file

@ -1,13 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getNonce = void 0;
'use strict'
Object.defineProperty(exports, '__esModule', { value: true })
exports.getNonce = void 0
function getNonce() {
let text = '';
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let text = ''
const possible =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
for (let i = 0; i < 32; i++) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
text += possible.charAt(Math.floor(Math.random() * possible.length))
}
return text;
return text
}
exports.getNonce = getNonce;
exports.getNonce = getNonce
//# sourceMappingURL=nonce.js.map