Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.SourceSurfaceFragment.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel SourceSurfaceFragment class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Surface fragment for editing surfaces in source mode.
9 *
10 * @class
11 * @extends ve.dm.SurfaceFragment
12 *
13 * @constructor
14 * @param {ve.dm.Document} doc
15 */
16 ve.dm.SourceSurfaceFragment = function VeDmSourceSurfaceFragment() {
17 // Parent constructors
18 ve.dm.SourceSurfaceFragment.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.dm.SourceSurfaceFragment, ve.dm.SurfaceFragment );
24
25 /**
26 * @inheritdoc
27 */
28 ve.dm.SourceSurfaceFragment.prototype.annotateContent = function () {
29 const text = this.getText( true );
30
31 this.pushPending( this.convertFromSource( text ).then( ( selectionDocument ) => {
32 const tempSurfaceModel = new ve.dm.Surface( selectionDocument );
33 const tempFragment = tempSurfaceModel.getLinearFragment(
34 // TODO: Find content offsets
35 selectionDocument.getDocumentRange()
36 );
37 tempFragment.annotateContent.apply( tempFragment, arguments );
38
39 this.clearPending();
40 this.insertDocument( tempFragment.getDocument() );
41 return this.getPending();
42 } ) );
43
44 return this;
45 };
46
47 /**
48 * @inheritdoc
49 */
50 ve.dm.SourceSurfaceFragment.prototype.getAnnotations = function () {
51 // Source surface contains no annotations
52 return new ve.dm.AnnotationSet( this.getDocument().getStore() );
53 };
54
55 /**
56 * @inheritdoc
57 */
58 ve.dm.SourceSurfaceFragment.prototype.convertNodes = function () {
59 const text = this.getText( true );
60
61 this.pushPending( this.convertFromSource( text ).then( ( selectionDocument ) => {
62 const tempSurfaceModel = new ve.dm.Surface( selectionDocument );
63 const tempFragment = tempSurfaceModel.getLinearFragment(
64 // TODO: Find content offsets
65 selectionDocument.getDocumentRange()
66 );
67 tempFragment.convertNodes.apply( tempFragment, arguments );
68
69 this.clearPending();
70 this.insertDocument( tempFragment.getDocument() );
71 return this.getPending();
72 } ) );
73
74 return this;
75 };
76
77 /**
78 * @inheritdoc
79 */
80 ve.dm.SourceSurfaceFragment.prototype.insertContent = function ( content, annotate ) {
81 if ( typeof content !== 'string' ) {
82 const data = new ve.dm.ElementLinearData( new ve.dm.HashValueStore(), content );
83 // Pass `annotate` as `ignoreCoveringAnnotations`. If matching the target annotation (plain text) strip covering annotations.
84 if ( !data.isPlainText( null, false, [ 'paragraph' ], annotate ) ) {
85 this.insertDocument( new ve.dm.Document( content.concat( { type: 'internalList' }, { type: '/internalList' } ) ) );
86 return this;
87 }
88 } else {
89 content = ve.dm.sourceConverter.getDataFromSourceText( content, true );
90 }
91
92 // Parent method
93 return ve.dm.SourceSurfaceFragment.super.prototype.insertContent.call( this, content );
94 };
95
96 /**
97 * @inheritdoc
98 */
99 ve.dm.SourceSurfaceFragment.prototype.insertDocument = function ( doc, newDocRange, annotate ) {
100 const range = this.getSelection().getCoveringRange();
101
102 if ( !range ) {
103 return this;
104 }
105
106 if ( newDocRange ) {
107 doc = doc.shallowCloneFromRange( newDocRange );
108 newDocRange = doc.originalRange;
109 }
110
111 // Pass `annotate` as `ignoreCoveringAnnotations`. If matching the target annotation (plain text) strip covering annotations.
112 if ( doc.data.isPlainText( newDocRange, false, [ 'paragraph' ], annotate ) ) {
113 const data = doc.data.getDataSlice( newDocRange );
114 for ( let i = 0, l = data.length; i < l; i++ ) {
115 // Remove any text annotations, as we have determined them to be covering
116 if ( Array.isArray( data[ i ] ) ) {
117 data[ i ] = data[ i ][ 0 ];
118 }
119 // Convert newlines to paragraph breaks (T156498)
120 if ( data[ i ] === '\r' ) {
121 data.splice( i, 1 );
122 i--;
123 l--;
124 } else if ( data[ i ] === '\n' ) {
125 data.splice( i, 1, { type: '/paragraph' }, { type: 'paragraph' } );
126 i++;
127 l++;
128 } else if ( data[ i ] === '\u00a0' ) {
129 // Replace non-breaking spaces, they were probably created during HTML conversion
130 // and are invisible to the user so likely to just cause problems.
131 // If you want a non-breaking space in source you should probably use &nbsp; T154382
132 data[ i ] = ' ';
133 }
134 }
135 return ve.dm.SourceSurfaceFragment.super.prototype.insertContent.call( this, data );
136 }
137
138 this.pushPending(
139 this.convertToSource( doc ).then(
140 ( source ) => {
141 this.insertContent( source.trim() );
142 },
143 function () {
144 ve.error( 'Failed to convert document', arguments );
145 return ve.createDeferred().reject().promise();
146 }
147 )
148 );
149
150 return this;
151 };
152
153 /**
154 * @inheritdoc
155 */
156 ve.dm.SourceSurfaceFragment.prototype.wrapAllNodes = function ( wrapOuter, wrapEach ) {
157 const range = this.getSelection().getCoveringRange();
158
159 if ( !range ) {
160 return this;
161 }
162
163 function getOpening( element ) {
164 element.internal = {
165 whitespace: [ '\n', '\n', '\n', '\n' ]
166 };
167 return element;
168 }
169
170 function getClosing( element ) {
171 return { type: '/' + element.type };
172 }
173
174 if ( !Array.isArray( wrapOuter ) ) {
175 wrapOuter = [ wrapOuter ];
176 }
177
178 wrapEach = wrapEach || [];
179
180 if ( !Array.isArray( wrapEach ) ) {
181 wrapEach = [ wrapEach ];
182 }
183
184 const nodes = this.getSelectedLeafNodes();
185
186 const content = wrapOuter.map( getOpening );
187 for ( let i = 0; i < nodes.length; i++ ) {
188 const node = nodes[ i ];
189 ve.batchPush( content, wrapEach.map( getOpening ) );
190 ve.batchPush( content, this.getSurface().getLinearFragment( node.getRange() ).getText().split( '' ) );
191 ve.batchPush( content, wrapEach.reverse().map( getClosing ) );
192 }
193 ve.batchPush( content, wrapOuter.reverse().map( getClosing ) );
194
195 this.insertContent( content );
196
197 return this;
198 };
199
200 /**
201 * Convert model slice to source text
202 *
203 * The default implementation converts to HTML synchronously.
204 *
205 * If the conversion is asynchronous it should lock the surface
206 * until complete.
207 *
208 * @param {ve.dm.Document} doc
209 * @return {jQuery.Promise} Promise which resolves with source, or rejects
210 */
211 ve.dm.SourceSurfaceFragment.prototype.convertToSource = function ( doc ) {
212 if ( !doc.data.hasContent() ) {
213 return ve.createDeferred().resolve( '' ).promise();
214 } else {
215 return ve.createDeferred().resolve(
216 ve.properInnerHtml(
217 ve.dm.converter.getDomFromModel( doc ).body
218 )
219 ).promise();
220 }
221 };
222
223 /**
224 * Convert source text to model slice
225 *
226 * The default implementation converts from HTML synchronously.
227 *
228 * If the conversion is asynchronous it should lock the surface
229 * until complete.
230 *
231 * @param {string} source Source text
232 * @return {jQuery.Promise} Promise which resolves with document model
233 */
234 ve.dm.SourceSurfaceFragment.prototype.convertFromSource = function ( source ) {
235 const lang = this.getDocument().getLang(),
236 dir = this.getDocument().getDir();
237
238 if ( !source ) {
239 return ve.createDeferred().resolve(
240 new ve.dm.Document(
241 [
242 { type: 'paragraph', internal: { generated: 'wrapper' } }, { type: '/paragraph' },
243 { type: 'internalList' }, { type: '/internalList' }
244 ],
245 null, null, null, null,
246 lang, dir
247 )
248 ).promise();
249 } else {
250 return ve.createDeferred().resolve(
251 ve.dm.converter.getModelFromDom(
252 ve.createDocumentFromHtml( source, { lang: lang, dir: dir } )
253 )
254 ).promise();
255 }
256 };