Persist Realtime Preview state in a user preference

Store the current state (enabled/disabled) of Realtime Preview in
a new user preference `wikieditor-realtimepreview` and set it
when enabling or disabling the preview pane.

Bug: T294599
Change-Id: Id5f51c6d77ed0077906d5d5178cbfa785cc04dcf
This commit is contained in:
Sam Wilson 2022-03-31 13:03:18 +08:00
parent fe81f28aae
commit e6b77fb2ab
4 changed files with 32 additions and 2 deletions

View file

@ -350,7 +350,8 @@
]
},
"DefaultUserOptions": {
"usebetatoolbar": true
"usebetatoolbar": true,
"wikieditor-realtimepreview": 0
},
"RawHtmlMessages": [
"wikieditor-toolbar-help-heading-description",

View file

@ -345,6 +345,9 @@ class Hooks implements
'help-message' => 'wikieditor-toolbar-preference-help',
'section' => 'editing/editor',
];
$defaultPreferences['wikieditor-realtimepreview'] = [
'type' => 'api',
];
}
/**

View file

@ -7,7 +7,9 @@ var ErrorLayout = require( './ErrorLayout.js' );
*/
function RealtimePreview() {
this.configData = mw.loader.moduleRegistry[ 'ext.wikiEditor' ].script.files[ 'data.json' ];
this.enabled = false;
// Preference name, must match what's in extension.json and Hooks.php.
this.prefName = 'wikieditor-realtimepreview';
this.enabled = this.getUserPref();
this.twoPaneLayout = new TwoPaneLayout();
this.pagePreview = require( 'mediawiki.page.preview' );
// @todo This shouldn't be required, but the preview element is added in PHP
@ -59,6 +61,26 @@ RealtimePreview.prototype.getToolbarButton = function ( context ) {
return this.button;
};
/**
* Get the user preference for Realtime Preview.
*
* @public
* @return {boolean}
*/
RealtimePreview.prototype.getUserPref = function () {
return mw.user.options.get( this.prefName ) > 0;
};
/**
* Enable Realtime Preview.
*
* @public
*/
RealtimePreview.prototype.enable = function () {
this.enabled = false;
this.toggle();
};
/**
* Toggle the two-pane preview display.
*
@ -103,6 +125,7 @@ RealtimePreview.prototype.toggle = function () {
// Record the toggle state and update the button.
this.enabled = !this.enabled;
this.button.setFlags( { progressive: this.enabled } );
( new mw.Api() ).saveOption( this.prefName, this.enabled ? 1 : 0 );
};
/**

View file

@ -13,4 +13,7 @@ mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
}
}
} );
if ( realtimePreview.getUserPref() ) {
realtimePreview.enable();
}
} );