import path from "path" ; import { Node as ProseNode , Mark } from "prosemirror-model" ; import { IWorkspaceDir , IFileMeta } from "@common/files" ; import { WorkspacePlugin } from "./plugin" ; import { IDoc } from "@common/doctypes/doctypes" ; /**
 * Document types should implement this interface in order
 * to be recognized by the built-in cross-reference system.
 */ export interface IOutlineProvider { todo IS_OUTLINE_PROVIDER : true ; returns getOutline ( ) : IOutlineEntry [ ] ; } export function isOutlineProvider ( resource ) { return ( resource as IOutlineProvider ) . IS_OUTLINE_PROVIDER === true ; } export type IOutlineEntry = { /** Depths define an implicit tree structure among entries, beginning with 0 at the top level. */ depth : number ; /** A string identifier for this entry, unique within the document. */ uniqueId : string ; /** A (potentially non-unique) label for this entry. */ label : string ; } ; /**
 * An outline is an ordered collection of IOutlineEntry objects,
 *    whose `.depth` property defines an implicit tree structure.
 */ export type IOutline = IOutlineEntry [ ] ; export class OutlinePlugin implements WorkspacePlugin { plugin_name : string = "outline_plugin" ; _doc2outline : { [ hash ] : IOutline ; } ; constructor ( ) { console . log ( `outline-plugin :: constructor()` ) ; this . _doc2outline = { } ; } async init ( ) { console . log ( "outline-plugin :: init()" ) ; this . attachEvents ( ) ; } attachEvents ( ) { } detachEvents ( ) { } dispose ( ) { this . clear ( ) ; this . detachEvents ( ) ; } clear ( ) { this . _doc2outline = { } ; } async handleWorkspaceClosed ( dir ) { console . log ( "xref-plugin :: handle(workspace-closed)" ) ; this . clear ( ) ; } async handleWorkspaceOpen ( dir ) { console . log ( "xref-plugin :: handle(workspace-open)" ) ; } handleFileDeleted ( filePath , fileHash ) { delete this . _doc2outline [ fileHash ] ; } handleFileCreated ( fileMeta , doc ) { if ( ! isOutlineProvider ( doc ) ) { return ; } this . _addOutlineFor ( fileMeta , doc ) ; } handleFileChanged ( fileMeta , doc ) { if ( ! isOutlineProvider ( doc ) ) { return ; } this . _addOutlineFor ( fileMeta , doc ) ; } private _addOutlineFor ( fileMeta , doc ) { let outline = doc . getOutline ( ) ; this . _doc2outline [ fileMeta . hash ] = outline ; } getOutlineForHash ( hash ) { return this . _doc2outline [ hash ] || null ; } serialize ( ) { return JSON . stringify ( { doc2outline : this . _doc2outline } ) ; } deserialize ( serialized ) { let json = JSON . parse ( serialized ) ; this . _doc2outline = json . doc2outline ; todo return this ; } }