/**
 * 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 , collectElements , revealOrStartDidactByURI , getActualUri } from './extensionFunctions' ; import { HeadingNode } from './nodeProvider' ; import * as didactExtension from './extension' ; export const DEFAULT_TITLE_VALUE = `Didact Tutorial` ; export const VIEW_TYPE = 'didact' ; export class DidactManager { private static _instance : DidactManager ; private _panels : DidactPanel [ ] = [ ] ; private context : ExtensionContext | undefined = undefined ; private constructor ( ) { } public static get Instance ( ) : DidactManager { return this . _instance || ( this . _instance = new this ( ) ) ; } public async create ( didactUri , column ? ) { if ( didactUri ) { let panel ; 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 ) ; } await didactExtension . getTelemetry ( ) . sendDidactOpenTypeTracking ( didactUri . fsPath ) ; return panel ; } return undefined ; } public add ( panel ) { if ( panel ) { this . _panels . push ( panel ) ; } } public remove ( panel ) { if ( panel ) { const found = this . _panels . indexOf ( panel ) ; if ( found >= 0 ) { this . _panels . splice ( found , 1 ) ; } } } public active ( ) { return this . _panels . find ( p => p . visible ) ; } public resetVisibility ( ) { this . _panels . forEach ( p => p . visible = false ) ; } public setContext ( ctxt ) { this . context = ctxt ; } public getExtensionPath ( ) { if ( this . context ) { return this . context . extensionPath ; } return undefined ; } public countPanels ( ) { return this . _panels . length ; } public getByUri ( testUri ) { let returnPanel = undefined ; for ( const p of this . _panels . values ( ) ) { const originalUri = p . getDidactUriPath ( ) ; if ( testUri && originalUri && ( originalUri . toString ( ) === testUri . toString ( ) || ( originalUri ?. fsPath === testUri . fsPath ) ) ) { returnPanel = p ; break ; } } return returnPanel ; } public async openHeading ( node ) { if ( node . uri ) { const parentUri = getActualUri ( node . uri ) ; if ( parentUri ) { await revealOrStartDidactByURI ( parentUri ) ; } } if ( didactManager . active ( ) ) { const html = didactManager . active ( ) ?. getCurrentHTML ( ) ; const label = node . label as string ; if ( html && label ) { const tags = [ 'h1' , 'h2' , 'h3' , 'h4' ] ; let element = undefined ; let tag = undefined ; tags . forEach ( tagElement => { const foundElements = collectElements ( tagElement , html ) . filter ( heading => heading . textContent . match ( label ) ) ; if ( foundElements && foundElements . length > 0 && ! element ) { element = foundElements [ 0 ] ; tag = tagElement ; } } ) ; if ( element && tag ) { await didactManager . active ( ) ?. sendScrollToHeadingMessage ( tag , element . rawText ) ; } } } } } export const didactManager = DidactManager . Instance ;