From 71bcad078f75c2511df70a7735e4bba39a4dd273 Mon Sep 17 00:00:00 2001 From: osvaldopina Date: Sat, 3 Aug 2019 09:53:22 -0300 Subject: [PATCH] remember last selected view --- .../chrome-extension/src/app/app.component.ts | 8 +++- .../chrome-extension/src/app/app.module.ts | 12 ----- .../src/app/manifest/dev/manifest.json | 3 +- .../src/app/manifest/prod/manifest.json | 3 +- projects/chrome-extension/src/main.ts | 29 ++++++------ .../hal-form-template.component.ts | 1 + .../src/lib/hal-render.component.ts | 45 ++++++++++++++----- 7 files changed, 61 insertions(+), 40 deletions(-) diff --git a/projects/chrome-extension/src/app/app.component.ts b/projects/chrome-extension/src/app/app.component.ts index 702d370..0366f82 100644 --- a/projects/chrome-extension/src/app/app.component.ts +++ b/projects/chrome-extension/src/app/app.component.ts @@ -1,11 +1,14 @@ -import { Component } from '@angular/core'; +import { Component, ChangeDetectorRef, ChangeDetectionStrategy, OnInit, Inject } from '@angular/core'; @Component({ selector: 'app-root', - template: '', + template: '', }) export class AppComponent { + constructor(@Inject('initialView') public initialView: string) { + } + jsonValue = this.getPreJsonElement().textContent; getPreJsonElement(): ChildNode { @@ -19,4 +22,5 @@ export class AppComponent { return preElement; } + } diff --git a/projects/chrome-extension/src/app/app.module.ts b/projects/chrome-extension/src/app/app.module.ts index 06db4f3..f424822 100644 --- a/projects/chrome-extension/src/app/app.module.ts +++ b/projects/chrome-extension/src/app/app.module.ts @@ -27,16 +27,4 @@ export class AppModule { document.body.childNodes[1].remove(); } - checkFont(strFamily) { - const objDiv = document.createElement('div'); - - objDiv.style.fontFamily = strFamily; - objDiv.appendChild(document.createTextNode('FONT TEST')); - - if (window.getComputedStyle) { - return window.getComputedStyle(objDiv, null).getPropertyValue('font-family') === strFamily; - } - - return objDiv.style.fontFamily === strFamily; - } } diff --git a/projects/chrome-extension/src/app/manifest/dev/manifest.json b/projects/chrome-extension/src/app/manifest/dev/manifest.json index 2f712f6..631347b 100644 --- a/projects/chrome-extension/src/app/manifest/dev/manifest.json +++ b/projects/chrome-extension/src/app/manifest/dev/manifest.json @@ -34,6 +34,7 @@ ], "permissions": [ "*://*/*", - "" + "", + "storage" ] } diff --git a/projects/chrome-extension/src/app/manifest/prod/manifest.json b/projects/chrome-extension/src/app/manifest/prod/manifest.json index 0152a7a..2e34d00 100644 --- a/projects/chrome-extension/src/app/manifest/prod/manifest.json +++ b/projects/chrome-extension/src/app/manifest/prod/manifest.json @@ -33,6 +33,7 @@ ], "permissions": [ "*://*/*", - "" + "", + "storage" ] } diff --git a/projects/chrome-extension/src/main.ts b/projects/chrome-extension/src/main.ts index cc14665..e10a275 100644 --- a/projects/chrome-extension/src/main.ts +++ b/projects/chrome-extension/src/main.ts @@ -3,7 +3,7 @@ import '@webcomponents/custom-elements'; import 'core-js/es7/reflect'; import 'zone.js/dist/zone'; -import { enableProdMode, NgModuleRef } from '@angular/core'; +import { enableProdMode } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { AppModule } from './app/app.module'; @@ -11,7 +11,6 @@ import { environment } from './environments/environment'; document.addEventListener('DOMContentLoaded', () => { - if (halJsonPage()) { const json = getJson(); if (json) { @@ -20,19 +19,19 @@ document.addEventListener('DOMContentLoaded', () => { changeBodyMargin(); } } - }, false); function bootstrapComponent() { - if (environment.production) { enableProdMode(); } - platformBrowserDynamic().bootstrapModule(AppModule) - .catch((err) => { - console.log('error bootstraping plugin:' + err); - }); + getInitialView().then((initialView) => { + platformBrowserDynamic([{ provide: 'initialView', useValue: initialView}]).bootstrapModule(AppModule) + .catch((err) => { + console.log('error bootstraping plugin:' + err); + }); + }); } function halJsonPage() { @@ -46,18 +45,14 @@ function halJsonPage() { } function isJsonPage() { - return documentHasOnlyHtmlWithHeadAndBody() && bodyHasOnlyPre(); - } function documentHasOnlyHtmlWithHeadAndBody() { - return document.children.length === 1 && document.childNodes[0].nodeName.toUpperCase() === 'HTML' && document.children[0].children.length === 2 && document.children[0].childNodes[0].nodeName.toUpperCase() === 'HEAD' && document.children[0].childNodes[1].nodeName.toUpperCase() === 'BODY'; - } function bodyHasOnlyPre() { @@ -66,13 +61,11 @@ function bodyHasOnlyPre() { } function getJson() { - try { return JSON.parse(document.body.firstChild.textContent); } catch (err) { return null; } - } function addDocType() { @@ -91,3 +84,11 @@ function addDocType() { function changeBodyMargin() { document.body.style.margin = '0px'; } + +function getInitialView(): Promise { + return new Promise((resolve, reject) => { + chrome.storage.local.get(['hal-render-view'], (value) => { + resolve(value['hal-render-view']); + }); + }); +} diff --git a/projects/hal-form-template/src/lib/hal-form-template/hal-form-template.component.ts b/projects/hal-form-template/src/lib/hal-form-template/hal-form-template.component.ts index 069d2f0..554c26d 100644 --- a/projects/hal-form-template/src/lib/hal-form-template/hal-form-template.component.ts +++ b/projects/hal-form-template/src/lib/hal-form-template/hal-form-template.component.ts @@ -135,4 +135,5 @@ export class HalFormTemplateComponent implements OnChanges { hideLoader() { this.loading = false; } + } diff --git a/projects/hal-render-component/src/lib/hal-render.component.ts b/projects/hal-render-component/src/lib/hal-render.component.ts index 04ae32d..3113e2e 100644 --- a/projects/hal-render-component/src/lib/hal-render.component.ts +++ b/projects/hal-render-component/src/lib/hal-render.component.ts @@ -17,11 +17,19 @@ export class HalRenderComponent { root: JsonElementNode; - json: String; + json: string; - currentView = CurrentView.TREE; + curView: CurrentView; - isAmudsenHalForm = false; + init: string; + + + @Input() + initialView: string; + + currentView: CurrentView = null; + + isAmundsenHalForm = false; @Input() set hal(value: string) { @@ -31,11 +39,23 @@ export class HalRenderComponent { this.json = value; this.root = buildHalJsonTree(jsonValue, new Curies(), true); this.expandAll(); - this.isAmudsenHalForm = isAmundsenHalForm(jsonValue); - this.currentView = CurrentView.TREE; + this.isAmundsenHalForm = isAmundsenHalForm(jsonValue); + if (this.currentView === null) { + if (this.initialView === CurrentView.FORM && !this.isAmundsenHalForm) { + this.currentView = CurrentView.TREE; + } else if (this.initialView === null || this.initialView === undefined) { + this.currentView = CurrentView.TREE; + } else { + this.currentView = CurrentView[this.initialView]; + } + } } } + get hal(): string { + return this.json; + } + expandAll() { if (this.root) { this.expand(this.root); @@ -49,19 +69,24 @@ export class HalRenderComponent { } viewTree() { - this.currentView = CurrentView.TREE; + this.setView(CurrentView.TREE); } viewRaw() { - this.currentView = CurrentView.RAW; + this.setView(CurrentView.RAW); } viewForm() { - this.currentView = CurrentView.FORM; + this.setView(CurrentView.FORM); + } + + setView(view: CurrentView) { + this.currentView = view; + chrome.storage.local.set({ 'hal-render-view': this.currentView }); } showRawButton(): boolean { - return this.currentView !== CurrentView.RAW; + return this.currentView !== CurrentView.RAW; } showTreeButton(): boolean { @@ -69,7 +94,7 @@ export class HalRenderComponent { } showFormButton(): boolean { - return this.currentView !== CurrentView.FORM && this.isAmudsenHalForm; + return this.currentView !== CurrentView.FORM && this.isAmundsenHalForm; } isCurrentViewTree() {