Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ce/ve.ce.MWWikitextSurface.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel Surface class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel surface.
9 *
10 * @class
11 * @extends ve.ce.Surface
12 *
13 * @constructor
14 * @param {ve.dm.Surface} model
15 * @param {ve.ui.Surface} ui
16 * @param {Object} [config]
17 */
18 ve.ce.MWWikitextSurface = function VeCeMwWikitextSurface() {
19 // Parent constructors
20 ve.ce.MWWikitextSurface.super.apply( this, arguments );
21
22 this.pasteTargetInput = new OO.ui.MultilineTextInputWidget();
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.ce.MWWikitextSurface, ve.ce.Surface );
28
29 /**
30 * @inheritdoc
31 */
32 ve.ce.MWWikitextSurface.prototype.onCopy = function ( e ) {
33 const clipboardData = e.originalEvent.clipboardData,
34 text = this.getModel().getFragment().getText( true ).replace( /\n\n/g, '\n' );
35
36 if ( !text ) {
37 return;
38 }
39
40 if ( clipboardData ) {
41 // Disable the default event so we can override the data
42 e.preventDefault();
43 clipboardData.setData( 'text/plain', text );
44 // We're not going to set HTML, but for browsers that support custom data, set a clipboard key
45 if ( ve.isClipboardDataFormatsSupported( e, true ) ) {
46 const slice = this.model.documentModel.shallowCloneFromSelection( this.getModel().getSelection() );
47 this.clipboardIndex++;
48 const clipboardKey = this.clipboardId + '-' + this.clipboardIndex;
49 this.clipboard = { slice: slice, hash: null };
50 // Clone the elements in the slice
51 slice.data.cloneElements( true );
52 clipboardData.setData( 'text/xcustom', clipboardKey );
53
54 // Explicitly store wikitext as text/x-wiki, so that wikitext-aware paste
55 // contexts can accept it without having to do any content-
56 // sniffing.
57 clipboardData.setData( 'text/x-wiki', text );
58 }
59 } else {
60 const originalSelection = new ve.SelectionState( this.nativeSelection );
61
62 // Save scroll position before changing focus to "offscreen" paste target
63 const scrollTop = this.surface.$scrollContainer.scrollTop();
64
65 // Prevent surface observation due to native range changing
66 this.surfaceObserver.disable();
67 this.$pasteTarget.empty().append( this.pasteTargetInput.$element );
68 this.pasteTargetInput.setValue( text ).select();
69
70 // Restore scroll position after changing focus
71 this.surface.$scrollContainer.scrollTop( scrollTop );
72
73 // setTimeout: postpone until after the default copy action
74 setTimeout( () => {
75 // Change focus back
76 this.$attachedRootNode[ 0 ].focus();
77 this.showSelectionState( originalSelection );
78 // Restore scroll position
79 this.surface.$scrollContainer.scrollTop( scrollTop );
80 this.surfaceObserver.clear();
81 this.surfaceObserver.enable();
82 // Detach input
83 this.pasteTargetInput.$element.detach();
84 } );
85 }
86 };
87
88 /**
89 * @inheritdoc
90 */
91 ve.ce.MWWikitextSurface.prototype.afterPasteInsertExternalData = function ( targetFragment, pastedDocumentModel, contextRange ) {
92 const wasSpecial = this.pasteSpecial,
93 // TODO: This check returns true if the paste contains meaningful structure (tables, lists etc.)
94 // but no annotations (bold, links etc.).
95 wasPlain = wasSpecial || pastedDocumentModel.data.isPlainText( contextRange, true, undefined, true );
96
97 const plainPastedDocumentModel = pastedDocumentModel.shallowCloneFromRange( contextRange );
98 plainPastedDocumentModel.data.sanitize( { plainText: true, keepEmptyContentBranches: true } );
99 // We just turned this into plaintext, which probably
100 // affected the content-length. Luckily, because of
101 // the earlier clone, we know we just want the whole
102 // document, and because of the major change to
103 // plaintext, the difference between originalRange and
104 // balancedRange don't really apply. As such, clear
105 // out newDocRange. (Can't just make it undefined;
106 // need to exclude the internal list, and since we're
107 // from a paste we also have to exclude the
108 // opening/closing paragraph.)
109 const plainContextRange = new ve.Range( plainPastedDocumentModel.getDocumentRange().from + 1, plainPastedDocumentModel.getDocumentRange().to - 1 );
110 this.pasteSpecial = true;
111
112 // isPlainText is true but we still need sanitize (e.g. remove lists)
113 const promise = ve.ce.MWWikitextSurface.super.prototype.afterPasteInsertExternalData.call( this, targetFragment, plainPastedDocumentModel, plainContextRange );
114 if ( !wasPlain ) {
115 promise.then( () => {
116 // We need to wait for the selection change after paste as that triggers
117 // a contextChange event. Really we should wait for the afterPaste promise to resolve.
118 setTimeout( () => {
119 const surface = this.getSurface(),
120 context = surface.getContext();
121 // Ensure surface is deactivated on mobile so context can be shown (T336073)
122 if ( context.isMobile() ) {
123 surface.getView().deactivate();
124 }
125 context.addPersistentSource( {
126 embeddable: false,
127 name: 'wikitextPaste',
128 data: {
129 doc: pastedDocumentModel,
130 contextRange: contextRange,
131 fragment: targetFragment
132 }
133 } );
134 surface.getModel().once( 'select', () => {
135 context.removePersistentSource( 'wikitextPaste' );
136 } );
137 } );
138 } );
139 }
140 return promise;
141 };