1- import { HistoryState , Notice , requireApiVersion , TAbstractFile , WorkspaceLeaf } from 'obsidian' ;
2- import { around } from "monkey-around" ;
3- import { LayoutStorage , Service , windowEvent } from "@ophidian/core" ;
1+ import { HistoryState , Notice , TAbstractFile , WorkspaceLeaf } from 'obsidian' ;
2+ import { around } from "monkey-around" ;
3+ import { LayoutStorage , Service } from "@ophidian/core" ;
44import { leafName } from './pane-relief' ;
55import { formatState } from './Navigator' ;
66
77const HIST_ATTR = "pane-relief:history-v1" ;
88const SERIAL_PROP = "pane-relief:history-v1" ;
9- export const hasTabHistory = requireApiVersion ( "0.16.3" ) ;
109
1110declare module "obsidian" {
1211 interface Workspace {
@@ -162,7 +161,6 @@ export class History {
162161
163162 pos : number
164163 stack : HistoryEntry [ ]
165- hadTabs = hasTabHistory ;
166164
167165 constructor ( public leaf ?: WorkspaceLeaf , { pos, stack} : SerializableHistory = { pos :0 , stack :[ ] } ) {
168166 if ( leaf ) leaf [ HIST_ATTR ] = this ; // prevent recursive lookups
@@ -173,7 +171,7 @@ export class History {
173171
174172 saveToNative ( ) : this {
175173 const nativeHistory = this . leaf ?. history ;
176- if ( ! nativeHistory || ! hasTabHistory ) return this ;
174+ if ( ! nativeHistory ) return this ;
177175 const stack = this . stack . map ( entry => entry . asNative ) ;
178176 nativeHistory . deserialize ( {
179177 backHistory : stack . slice ( this . pos + 1 ) . reverse ( ) ,
@@ -184,7 +182,7 @@ export class History {
184182
185183 loadFromNative ( ) : this {
186184 const history = this . leaf ?. history ;
187- if ( ! history || ! hasTabHistory ) return this ;
185+ if ( ! history ) return this ;
188186 const stack : typeof history . backHistory = [ ] . concat (
189187 history . forwardHistory . slice ( ) . filter ( s => s ) ,
190188 { state : { } , eState : { } } ,
@@ -232,7 +230,7 @@ export class History {
232230 // prevent wraparound
233231 const newPos = Math . max ( 0 , Math . min ( this . pos - by , this . stack . length - 1 ) ) ;
234232 if ( force || newPos !== this . pos ) {
235- if ( this . leaf . history && hasTabHistory ) {
233+ if ( this . leaf . history ) {
236234 this . pos = newPos ;
237235 this . leaf . history . go ( by ) ;
238236 } else this . goto ( newPos ) ;
@@ -280,7 +278,7 @@ export class HistoryManager extends Service {
280278 }
281279 } ) ) ;
282280
283- if ( hasTabHistory ) {
281+ if ( true ) {
284282 // Forward native tab history events to our own implementation
285283 this . register ( around ( WorkspaceLeaf . prototype , {
286284 trigger ( old ) { return function trigger ( name , ...data ) {
@@ -294,65 +292,7 @@ export class HistoryManager extends Service {
294292 } ) ) ;
295293
296294 // Incorporate any prior history state (e.g. on plugin update)
297- if ( app . workspace . layoutReady ) app . workspace . iterateAllLeaves ( leaf => { History . forLeaf ( leaf ) ; } )
298-
299- // Skip most actual history replacement if native history is tab-based
300- return ;
295+ app . workspace . onLayoutReady ( ( ) => app . workspace . iterateAllLeaves ( leaf => { leaf . trigger ( "history-change" ) } ) )
301296 }
302-
303- // Monkeypatch: check for popstate events (to suppress them)
304- this . register ( around ( WorkspaceLeaf . prototype , {
305- setViewState ( old ) { return function setViewState ( vs , es ) {
306- if ( vs . popstate && window . event ?. type === "popstate" ) {
307- return Promise . resolve ( ) ;
308- }
309- return old . call ( this , vs , es ) ;
310- } }
311- } ) ) ;
312-
313- this . register ( around ( app . workspace , {
314- // Monkeypatch: keep Obsidian from pushing history in setActiveLeaf
315- setActiveLeaf ( old ) { return function setActiveLeaf ( leaf , ...etc : any [ ] ) {
316- const unsub = around ( this , {
317- recordHistory ( old ) { return function ( leaf : WorkspaceLeaf , _push : boolean , ...args : any [ ] ) {
318- // Always update state in place
319- return old . call ( this , leaf , false , ...args ) ;
320- } ; }
321- } ) ;
322- try {
323- return old . call ( this , leaf , ...etc ) ;
324- } finally {
325- unsub ( ) ;
326- }
327- } } ,
328- } ) ) ;
329-
330- function isSyntheticHistoryEvent ( button : number ) {
331- return ! ! windowEvent ( ( _ , event ) => {
332- if ( event . type === "mousedown" && ( event as MouseEvent ) . button === button ) {
333- event . preventDefault ( ) ;
334- event . stopImmediatePropagation ( ) ;
335- return true ;
336- }
337- } ) ;
338- }
339-
340- // Proxy the window history with a wrapper that delegates to the active leaf's History object,
341- const realHistory = window . history ;
342- this . register ( ( ) => ( window as any ) . history = realHistory ) ;
343- Object . defineProperty ( window , "history" , { enumerable : true , configurable : true , writable : true , value : {
344- get state ( ) { return History . current ( ) . state ; } ,
345- get length ( ) { return History . current ( ) . length ; } ,
346-
347- back ( ) { if ( ! isSyntheticHistoryEvent ( 3 ) ) this . go ( - 1 ) ; } ,
348- forward ( ) { if ( ! isSyntheticHistoryEvent ( 4 ) ) this . go ( 1 ) ; } ,
349- go ( by : number ) { History . current ( ) . go ( by ) ; } ,
350-
351- replaceState ( state : PushState , title : string , url : string ) { History . current ( ) . replaceState ( state , title , url ) ; } ,
352- pushState ( state : PushState , title : string , url : string ) { History . current ( ) . pushState ( state , title , url ) ; } ,
353-
354- get scrollRestoration ( ) { return realHistory . scrollRestoration ; } ,
355- set scrollRestoration ( val ) { realHistory . scrollRestoration = val ; } ,
356- } } ) ;
357297 }
358298}
0 commit comments