Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/dialogs/ve.ui.MWPreDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor user interface MWPreDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Dialog for editing MediaWiki preformatted text using `<pre>` tags.
10 *
11 * @class
12 * @extends ve.ui.MWExtensionDialog
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWPreDialog = function VeUiMWPreDialog() {
18 // Parent constructor
19 ve.ui.MWPreDialog.super.apply( this, arguments );
20
21 this.$element.addClass( 've-ui-mwPreDialog' );
22 };
23
24 /* Inheritance */
25
26 OO.inheritClass( ve.ui.MWPreDialog, ve.ui.MWExtensionDialog );
27
28 /* Static properties */
29
30 ve.ui.MWPreDialog.static.name = 'mwPre';
31
32 ve.ui.MWPreDialog.static.size = 'large';
33
34 ve.ui.MWPreDialog.static.title = OO.ui.deferMsg( 'visualeditor-mwpredialog-title' );
35
36 ve.ui.MWPreDialog.static.modelClasses = [ ve.dm.MWPreNode ];
37
38 ve.ui.MWPreDialog.static.actions = [
39 ...ve.ui.MWPreDialog.super.static.actions,
40 {
41 action: 'convert',
42 label: OO.ui.deferMsg( 'visualeditor-mwpredialog-convert' ),
43 modes: [ 'edit' ]
44 }
45 ];
46
47 /* Methods */
48
49 /**
50 * @inheritdoc
51 */
52 ve.ui.MWPreDialog.prototype.initialize = function () {
53 // Parent method
54 ve.ui.MWPreDialog.super.prototype.initialize.call( this );
55
56 // Properties
57 this.panel = new OO.ui.PanelLayout( {
58 padded: true
59 } );
60 // Note that this overrides this.input from ve.ui.MWExtensionWindow
61 this.input = new ve.ui.MWPreTextInputWidget( {
62 // This number doesn't really matter, it just needs to be large.
63 // The real height is enforced by #getBodyHeight and max-height in CSS.
64 rows: 100,
65 classes: [ 've-ui-mwExtensionWindow-input' ]
66 } );
67 this.input.connect( this, { resize: 'updateSize' } );
68
69 // Initialization
70 this.$element.addClass( 've-ui-mwPreDialog' );
71 this.panel.$element.append( this.input.$element );
72 this.$body.append( this.panel.$element );
73 };
74
75 /**
76 * @inheritdoc
77 */
78 ve.ui.MWPreDialog.prototype.getReadyProcess = function ( data ) {
79 return ve.ui.MWPreDialog.super.prototype.getReadyProcess.call( this, data )
80 .next( () => {
81 this.input.focus();
82 } );
83 };
84
85 /**
86 * @inheritdoc
87 */
88 ve.ui.MWPreDialog.prototype.getActionProcess = function ( action ) {
89 if ( action === 'convert' ) {
90 return new OO.ui.Process( () => {
91 const
92 value = this.input.getValue(),
93 nodeRange = this.selectedNode.getOuterRange(),
94 surfaceModel = this.getFragment().getSurface(),
95 doc = surfaceModel.getDocument(),
96 // Turn the text into content for insertion into document
97 content = value.split( '' ),
98 // Apply any annotations that were applied to the node also to the new content
99 annotations = doc.data.getAnnotationsFromOffset( nodeRange.start );
100 ve.dm.Document.static.addAnnotationsToData( content, annotations );
101 // Make it use preformatted text
102 content.unshift( { type: 'mwPreformatted' } );
103 content.push( { type: '/mwPreformatted' } );
104 // Replace
105 surfaceModel.change(
106 ve.dm.TransactionBuilder.static.newFromReplacement( doc, nodeRange, content )
107 );
108 this.close();
109 } );
110 }
111 // Parent method
112 return ve.ui.MWPreDialog.super.prototype.getActionProcess.call( this, action );
113 };
114
115 /**
116 * @inheritdoc
117 */
118 ve.ui.MWPreDialog.prototype.getBodyHeight = function () {
119 return 500;
120 };
121
122 /* Registration */
123
124 ve.ui.windowFactory.register( ve.ui.MWPreDialog );