Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
FUSETOOLS2-725 - Removing single window limit on didact (#365)
Browse files Browse the repository at this point in the history
* FUSETOOLS2-725 - Removing single window limit on didact

* updates to support single tutorial window
* fix for title changes
* fix issue with disposed webview when saving column
* adding first cut of new didact panel and manager
* first solid pass with most things working
* working through all tests and clean-up
* adding tests and updates after first round of feedback
* persist uri for easier identification
* also reuse old panels (based on uri) to avoid opening a new window each time
* add test to ensure that if we open the same didact URI twice we only end up with one panel
* combining if conditions

Signed-off-by: Brian Fitzpatrick <bfitzpat@redhat.com>
  • Loading branch information
bfitzpat authored Jan 29, 2021
1 parent 4dd54da commit 98c40e5
Show file tree
Hide file tree
Showing 21 changed files with 796 additions and 979 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to the "vscode-didact" extension will be documented in this
## 0.2.1

- Switching from xmldom to node-html-parser for heading parsing
- remove history functions
- open a new didact window with each tutorial
- persist html state between workspace sessions
- remove old webview implementation

## 0.2.0

Expand Down
3 changes: 1 addition & 2 deletions demos/asciidoc/didact-demo.didact.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ Asciidoc doesn't seem to support bringing in native HTML. But you can bring in t

## Limitations

* We can only open one Didact file at a time at the moment and there isn't the concept of a tutorial "history" to step forward or back through yet.
* Didact, because it utilizes the VS Code Webview, is limited to what files it can access. For example, local image files must exist in the same folder as the didact file or in a child folder.
* Didact, beacause it utilizes the VS Code Webview, is limited to what files it can access. For example, local image files must exist in the same folder as the didact file or in a child folder.
# Ideas or want to contribute?

Expand Down
3 changes: 1 addition & 2 deletions demos/markdown/didact-demo.didact.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ And you can even bring in tables...

## Limitations

* We can only open one Didact file at a time at the moment and there isn't the concept of a tutorial "history" to step forward or back through yet.
* Didact, because it utilizes the VS Code Webview, is limited to what files it can access. For example, local image files must exist in the same folder as the didact file or in a child folder.
* Didact, beacause it utilizes the VS Code Webview, is limited to what files it can access. For example, local image files must exist in the same folder as the didact file or in a child folder.

# Ideas or want to contribute?

Expand Down
37 changes: 32 additions & 5 deletions media/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ function () {
//connect to the vscode api
const vscode = acquireVsCodeApi();

const oldState = vscode.getState();
let oldBody = oldState ? oldState.oldbody : '';
if (oldBody) {
const textFromBase64 = window.btoa(oldBody);
document.body = decodeURI(textFromBase64);
}
let oldTitle = oldState ? oldState.oldTitle : '';
if (oldTitle) {
this.title = oldTitle;
}

document.body.addEventListener('click', event => {
let node = event && event.target;
while (node) {
Expand All @@ -41,6 +52,7 @@ function () {
element.checked = true;
if (document.body) {
vscode.postMessage({ command: 'update', text: document.body });
updateState();
}
}

Expand Down Expand Up @@ -70,17 +82,25 @@ function () {
return elements;
}

function updateState(passedUri) {
const textToCache = '<!DOCTYPE HTML>' + '\n' + document.documentElement.outerHTML;
const encodedText = encodeURI(textToCache);
const textToBase64 = window.btoa(encodedText);
vscode.setState( { oldBody: textToBase64, oldTitle : this.title, oldUri : passedUri });
}

// Handle messages sent from the extension to the webview
window.addEventListener('message', event => {
const message = event.data; // The json data that the extension sent
const json = JSON.parse(message);
switch (json.command) {
case 'requirementCheck':
const requirementName = json.requirementName;
const isAvailable = json.result;
const requirementName = json.requirementName;
const isAvailable = json.result;
const passedUri = json.oldUri;

let element = document.getElementById(requirementName);
let element = document.getElementById(requirementName);

switch (json.command) {
case 'requirementCheck':
// add check for adoc div/p requirement label
if (element.tagName.toLowerCase() === 'div' && element.childNodes.length > 0) {
let list = element.getElementsByTagName('em');
Expand All @@ -101,7 +121,9 @@ function () {
}
}
console.log(`${requirementName} is available: ${isAvailable}`);
updateState();
break;

case 'allRequirementCheck':
var links = collectElements("a");
for (let index = 0; index < links.length; index++) {
Expand All @@ -115,6 +137,11 @@ function () {
}
}
}
updateState();
break;

case 'setState':
updateState(passedUri);
break;
}
});
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

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

28 changes: 1 addition & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,6 @@
"command": "vscode.didact.verifyCommands",
"title": "Validate Didact File",
"when": "resourceFilename =~ /[.](didact)[.](md|adoc)$/"
},
{
"command": "vscode.didact.historyBack",
"title": "Didact: Show Previous Entry in the Didact History",
"when": "didact.webview"
},
{
"command": "vscode.didact.historyForward",
"title": "Didact: Show Next Entry in the Didact History",
"when": "didact.webview"
},
{
"command": "vscode.didact.clearHistory",
"title": "Didact: Clear History",
"when": "didact.webview"
}
],
"keybindings": [
Expand All @@ -132,18 +117,6 @@
"key": "ctrl+shift+v",
"mac": "cmd+shift+v",
"when": "editorFocus && resourceFilename =~ /[.](didact)[.](md|adoc)$/"
},
{
"command": "vscode.didact.historyBack",
"key": "alt+down",
"mac": "alt+down",
"when": "didact.webview"
},
{
"command": "vscode.didact.historyForward",
"key": "alt+up",
"mac": "alt+up",
"when": "didact.webview"
}
],
"menus": {
Expand Down Expand Up @@ -244,6 +217,7 @@
"@types/markdown-it": "^12.0.1",
"@types/request-promise": "^4.1.47",
"asciidoctor": "^2.2.1",
"base-64": "^1.0.0",
"download": "^8.0.0",
"glob": "^7.1.6",
"markdown-it": "^12.0.4",
Expand Down
12 changes: 0 additions & 12 deletions resources/didactCompletionCatalog.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
"fullCommandId":"didact.tutorials.focus",
"documentation": "Focuses VS Code on the Didact Tutorials view"
},
{
"fullCommandId":"vscode.didact.clearHistory",
"documentation": "Clears the Didact tutorial history"
},
{
"fullCommandId":"vscode.didact.cliCommandSuccessful",
"parms": ["Requirement-Label", "URLEncoded-Command-to-Execute"],
Expand Down Expand Up @@ -42,14 +38,6 @@
"parms": ["Requirement-Label", "Full-Extension-ID"],
"documentation": "Simple check to see if the extension Id is installed in the user workspace"
},
{
"fullCommandId":"vscode.didact.historyBack",
"documentation": "Move back one entry in the Didact tutorial history"
},
{
"fullCommandId":"vscode.didact.historyForward",
"documentation": "Move forward one entry in the Didact tutorial history"
},
{
"fullCommandId":"vscode.didact.openNamedOutputChannel",
"parms": ["Output-Channel-Name"],
Expand Down
121 changes: 121 additions & 0 deletions src/didactManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';
import {ExtensionContext, Uri, ViewColumn} from 'vscode';
import { DidactPanel } from './didactPanel';
import { isAsciiDoc } from './extensionFunctions';

export const DEFAULT_TITLE_VALUE = `Didact Tutorial`;
export const VIEW_TYPE = 'didact';

export class DidactManager {

// singleton instance
private static _instance: DidactManager;
private _panels: DidactPanel[] = [];
private context : ExtensionContext | undefined = undefined;

private constructor() {
// empty
}

public static get Instance() : DidactManager {
return this._instance || (this._instance = new this());
}

public async create(didactUri : Uri, column? : ViewColumn) : Promise<DidactPanel | undefined> {
if (didactUri) {
let panel : DidactPanel | undefined;
let reload = false;
if(this.getByUri(didactUri)) {
panel = this.getByUri(didactUri);
column = panel?.getColumn();
panel?._panel?.dispose();
reload = true;
}
panel = new DidactPanel(didactUri);
if (panel) {
if (!column) {
column = ViewColumn.Active;
}
panel.initWebviewPanel(column, didactUri);
panel.setDidactUriPath(didactUri);
panel.setIsAsciiDoc(isAsciiDoc());
panel.handleEvents();
await panel.configure(reload);
}
return panel;
}
return undefined;
}

public add(panel: DidactPanel): void {
if (panel) {
this._panels.push(panel);
}
}

public remove(panel: DidactPanel): void {
if (panel) {
const found = this._panels.indexOf(panel);
if (found >= 0) {
this._panels.splice(found, 1);
}
}
}

public active(): DidactPanel | undefined {
return this._panels.find(p => p.visible);
}

public resetVisibility() : void {
this._panels.forEach(p => p.visible = false);
}

public setContext(ctxt : ExtensionContext): void {
this.context = ctxt;
}

public getExtensionPath() : string | undefined {
if (this.context) {
return this.context.extensionPath;
}
return undefined;
}

// for test purposes
public countPanels() : number {
return this._panels.length;
}

public getByUri(testUri: Uri): DidactPanel | undefined {
let returnPanel = undefined;
for (let index = 0; index < this._panels.length; index++) {
const p = this._panels[index];
const originalUri = p.getDidactUriPath();
if (testUri && originalUri && originalUri.toString() === testUri.toString()) {
returnPanel = p;
break;
}
}
return returnPanel;
}
}

// export preview manager singleton
export const didactManager = DidactManager.Instance;
Loading

0 comments on commit 98c40e5

Please sign in to comment.