Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/dialogs/ve.ui.SpecialCharacterDialog.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface SpecialCharacterDialog class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Inspector for inserting special characters.
9 *
10 * @class
11 * @extends ve.ui.ToolbarDialog
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 */
16 ve.ui.SpecialCharacterDialog = function VeUiSpecialCharacterDialog() {
17 // Parent constructor
18 ve.ui.SpecialCharacterDialog.super.apply( this, arguments );
19
20 this.characterListLoaded = false;
21
22 this.$element.addClass( 've-ui-specialCharacterDialog' );
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.ui.SpecialCharacterDialog, ve.ui.ToolbarDialog );
28
29 /* Static properties */
30
31 ve.ui.SpecialCharacterDialog.static.name = 'specialCharacter';
32
33 // Invisible title for accessibility
34 ve.ui.SpecialCharacterDialog.static.title =
35 OO.ui.deferMsg( 'visualeditor-specialcharacter-button-tooltip' );
36
37 ve.ui.SpecialCharacterDialog.static.size = 'full';
38
39 ve.ui.SpecialCharacterDialog.static.padded = false;
40
41 ve.ui.SpecialCharacterDialog.static.activeSurface = true;
42
43 ve.ui.SpecialCharacterDialog.static.handlesSource = true;
44
45 /* Methods */
46
47 /**
48 * @inheritdoc
49 */
50 ve.ui.SpecialCharacterDialog.prototype.initialize = function () {
51 // Parent method
52 ve.ui.SpecialCharacterDialog.super.prototype.initialize.call( this );
53
54 this.characterListLayout = new ve.ui.SymbolListBookletLayout();
55 this.characterListLayout.connect( this, {
56 choose: 'onCharacterListChoose'
57 } );
58 // Character list is lazy-loaded the first time getSetupProcess runs
59
60 this.$body.append( this.characterListLayout.$element );
61 };
62
63 /**
64 * @inheritdoc
65 */
66 ve.ui.SpecialCharacterDialog.prototype.getSetupProcess = function ( data ) {
67 data = data || {};
68 return ve.ui.SpecialCharacterDialog.super.prototype.getSetupProcess.call( this, data )
69 .next( () => {
70 this.surface = data.surface;
71 this.surface.getModel().connect( this, { contextChange: 'onContextChange' } );
72
73 this.characterListLayout.$element.toggleClass( 've-ui-specialCharacterDialog-characterList-source', this.surface.getMode() === 'source' );
74
75 if ( !this.characterListLoaded ) {
76 this.characterListLoaded = true;
77
78 ve.init.platform.fetchSpecialCharList()
79 .then( ( symbolData ) => {
80 this.characterListLayout.setSymbolData( symbolData );
81 this.updateSize();
82 } );
83 }
84 } );
85 };
86
87 /**
88 * @inheritdoc
89 */
90 ve.ui.SpecialCharacterDialog.prototype.getTeardownProcess = function ( data ) {
91 data = data || {};
92 return ve.ui.SpecialCharacterDialog.super.prototype.getTeardownProcess.call( this, data )
93 .first( () => {
94 this.surface.getModel().disconnect( this );
95 this.surface = null;
96 } );
97 };
98
99 /**
100 * @inheritdoc
101 */
102 ve.ui.SpecialCharacterDialog.prototype.getReadyProcess = function ( data ) {
103 return ve.ui.SpecialCharacterDialog.super.prototype.getReadyProcess.call( this, data )
104 .next( () => {
105 const surface = this.surface;
106 // The dialog automatically receives focus after opening, move it back to the surface.
107 // (Make sure an existing selection is preserved. Why does focus() reset the selection? 🤦)
108 const previousSelection = surface.getModel().getSelection();
109 // On deactivated surfaces (e.g. those using nullSelectionOnBlur), the native selection is
110 // removed after a setTimeout to fix a bug in iOS (T293661, in ve.ce.Surface#deactivate).
111 // Ensure that we restore the selection **after** this happens, otherwise the surface will
112 // get re-blurred. (T318720)
113 setTimeout( () => {
114 surface.getView().focus();
115 if ( !previousSelection.isNull() ) {
116 surface.getModel().setSelection( previousSelection );
117 }
118 } );
119 } );
120 };
121
122 /**
123 * @inheritdoc
124 */
125 ve.ui.SpecialCharacterDialog.prototype.getActionProcess = function ( action ) {
126 return new OO.ui.Process( () => {
127 this.close( { action: action } );
128 } );
129 };
130
131 /**
132 * Handle context change events from the surface model
133 */
134 ve.ui.SpecialCharacterDialog.prototype.onContextChange = function () {
135 this.setDisabled( !( this.surface.getModel().getSelection() instanceof ve.dm.LinearSelection ) );
136 };
137
138 /**
139 * Handle a character being chosen from the list
140 *
141 * @param {Object} character Character data
142 */
143 ve.ui.SpecialCharacterDialog.prototype.onCharacterListChoose = function ( character ) {
144 const fragment = this.surface.getModel().getFragment(),
145 mode = this.surface.getMode();
146
147 function encode( text ) {
148 if ( mode === 'visual' && character.entities ) {
149 return ve.init.platform.decodeEntities( text );
150 } else {
151 return text;
152 }
153 }
154
155 if ( character ) {
156 if ( typeof character === 'string' || character.string ) {
157 fragment.insertContent( encode( character.string || character ), true ).collapseToEnd().select();
158 } else if ( character.action.type === 'replace' ) {
159 fragment.insertContent( encode( character.action.options.peri ), true ).collapseToEnd().select();
160 } else if ( character.action.type === 'encapsulate' ) {
161 fragment.collapseToStart().insertContent( encode( character.action.options.pre ), true );
162 fragment.collapseToEnd().insertContent( encode( character.action.options.post ), true ).collapseToEnd().select();
163 }
164
165 ve.track(
166 'activity.' + this.constructor.static.name,
167 { action: 'insert-' + this.characterListLayout.currentPageName }
168 );
169 }
170 };
171
172 ve.ui.SpecialCharacterDialog.prototype.getBodyHeight = function () {
173 return 150;
174 };
175
176 ve.ui.SpecialCharacterDialog.prototype.getContentHeight = function () {
177 // Skip slow complicated measurements that always return 0 for this window
178 return this.getBodyHeight();
179 };
180
181 /* Registration */
182
183 ve.ui.windowFactory.register( ve.ui.SpecialCharacterDialog );