import { IWorkspaceDir , IFileMeta } from "@common/files" ; import { WorkspacePlugin } from "./plugin" ; import { IDoc } from "@common/doctypes/doctypes" ; /**
 * Type representing file metadata.
 */ export type IMetadata = { [ key ] : string | string [ ] ; } ; /**
 * Document types should implement this interface in order
 * to be recognized by the built-in cross-reference system.
 */ export interface IMetadataProvider { todo IS_METADATA_PROVIDER : true ; getMetadata ( ) : IMetadata ; } export function isMetadataProvider ( resource ) { return ( resource as IMetadataProvider ) . IS_METADATA_PROVIDER === true ; } export class MetadataPlugin implements WorkspacePlugin { plugin_name : string = "metadata_plugin" ; _doc2meta : { [ hash ] : IMetadata ; } ; constructor ( ) { console . log ( `metadata-plugin :: constructor()` ) ; this . _doc2meta = { } ; } async init ( ) { console . log ( "metadata-plugin :: init()" ) ; this . attachEvents ( ) ; } attachEvents ( ) { } detachEvents ( ) { } dispose ( ) { this . clear ( ) ; this . detachEvents ( ) ; } clear ( ) { this . _doc2meta = { } ; } async handleWorkspaceClosed ( dir ) { console . log ( `${ this . plugin_name } :: handle(workspace-closed)` ) ; this . clear ( ) ; } async handleWorkspaceOpen ( dir ) { console . log ( `${ this . plugin_name } :: handle(workspace-open)` ) ; } handleFileDeleted ( filePath , fileHash ) { delete this . _doc2meta [ fileHash ] ; } handleFileCreated ( fileMeta , doc ) { if ( ! isMetadataProvider ( doc ) ) { return ; } this . _addMetadataFor ( fileMeta , doc ) ; } handleFileChanged ( fileMeta , doc ) { if ( ! isMetadataProvider ( doc ) ) { return ; } this . _addMetadataFor ( fileMeta , doc ) ; } private _addMetadataFor ( fileMeta , doc ) { this . _doc2meta [ fileMeta . hash ] = doc . getMetadata ( ) ; } getMetadataForHash ( hash ) { return this . _doc2meta [ hash ] || null ; } serialize ( ) { return JSON . stringify ( { doc2meta : this . _doc2meta } ) ; } deserialize ( serialized ) { let json = JSON . parse ( serialized ) ; this . _doc2meta = json . doc2meta ; todo return this ; } }