Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/actions/ve.ui.MWWikitextAction.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWWikitextAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Content action.
9 *
10 * @class
11 * @extends ve.ui.Action
12 *
13 * @constructor
14 * @param {ve.ui.Surface} surface Surface to act on
15 */
16 ve.ui.MWWikitextAction = function VeUiMWWikitextAction() {
17 // Parent constructor
18 ve.ui.MWWikitextAction.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.ui.MWWikitextAction, ve.ui.Action );
24
25 /* Static Properties */
26
27 ve.ui.MWWikitextAction.static.name = 'mwWikitext';
28
29 ve.ui.MWWikitextAction.static.methods = [ 'toggleWrapSelection', 'wrapSelection', 'wrapLine' ];
30
31 /* Methods */
32
33 /**
34 * Wrap an selection inline
35 *
36 * @param {string} before Text to go before selection
37 * @param {string} after Text to go after selection
38 * @param {Function|string} placeholder Placeholder text to insert at an empty selection
39 * @param {Function} [expandOffsetsCallback] Function that returns a tuple of offsets to expand to selection to in order to get relevant text for unwrapping
40 * @param {Function} [unwrapOffsetsCallback] Function that returns a tuple of offsets to unwrap from the selected text,
41 * e.g. "''Foo'''" -> [2,3] to unwrap 2 from the left and 3 from the right
42 * @return {boolean} Action was executed
43 */
44 ve.ui.MWWikitextAction.prototype.toggleWrapSelection = function ( before, after, placeholder, expandOffsetsCallback, unwrapOffsetsCallback ) {
45 const originalFragment = this.surface.getModel().getFragment( null, false, true /* excludeInsertions */ );
46
47 let fragment = originalFragment;
48 let textBefore, textAfter;
49 if ( expandOffsetsCallback ) {
50 const contextRange = fragment.expandLinearSelection( 'siblings' ).getSelection().getCoveringRange();
51 const data = fragment.getDocument().data;
52 const range = fragment.getSelection().getCoveringRange();
53 textBefore = data.getText( true, new ve.Range( contextRange.start, range.start ) );
54 textAfter = data.getText( true, new ve.Range( range.end, contextRange.end ) );
55 const expandOffsets = expandOffsetsCallback( textBefore, textAfter );
56 if ( expandOffsets ) {
57 fragment = originalFragment.adjustLinearSelection( expandOffsets[ 0 ], expandOffsets[ 1 ] );
58 }
59 }
60
61 if ( unwrapOffsetsCallback ) {
62 const unwrapOffsets = unwrapOffsetsCallback( fragment.getText(), textBefore, textAfter );
63 if ( unwrapOffsets ) {
64 fragment.unwrapText( unwrapOffsets[ 0 ], unwrapOffsets[ 1 ] );
65 } else {
66 fragment.wrapText( before, after, placeholder, true );
67 }
68 originalFragment.select();
69 return true;
70 }
71
72 fragment.wrapText( before, after, placeholder ).select();
73 originalFragment.select();
74 return true;
75 };
76
77 /**
78 * Wrap an selection inline
79 *
80 * @param {string} before Text to go before selection
81 * @param {string} after Text to go after selection
82 * @param {Function|string} placeholder Placeholder text to insert at an empty selection
83 * @return {boolean} Action was executed
84 */
85 ve.ui.MWWikitextAction.prototype.wrapSelection = function ( before, after, placeholder ) {
86 const fragment = this.surface.getModel().getFragment( null, false, true /* excludeInsertions */ );
87 fragment.wrapText( before, after, placeholder ).select();
88 return true;
89 };
90
91 /**
92 * Wrap an selection as a block element on its own line
93 *
94 * If the selection is collapsed, it expands to take the whole line, otherwise it splits
95 * the paragraph to make sure it is one line
96 *
97 * @param {string} before Text to go before each line
98 * @param {string} after Text to go after each line
99 * @param {Function|string} placeholder Placeholder text to insert at an empty selection
100 * @param {Function} [unwrapOffsetsCallback] Function that returns a tuple of offsets to unwrap from the selected text,
101 * e.g. '== Foo ===' -> [2,3] to unwrap 2 from the left and 3 from the right
102 * @return {boolean} Action was executed
103 */
104 ve.ui.MWWikitextAction.prototype.wrapLine = function ( before, after, placeholder, unwrapOffsetsCallback ) {
105 let originalFragment = this.surface.getModel().getFragment( null, false, true /* excludeInsertions */ );
106 const selectedNodes = originalFragment.getLeafNodes();
107
108 let unwrapped = false;
109 for ( let i = selectedNodes.length - 1; i >= 0; i-- ) {
110 if ( selectedNodes.length > 1 && selectedNodes[ i ].nodeRange.isCollapsed() ) {
111 continue;
112 }
113 const fragment = this.surface.getModel().getLinearFragment( selectedNodes[ i ].nodeRange, true );
114 const unwrapOffsets = unwrapOffsetsCallback && unwrapOffsetsCallback( fragment.getText() );
115
116 if ( selectedNodes.length === 1 && originalFragment.getSelection().isCollapsed() ) {
117 originalFragment = fragment;
118 }
119
120 if ( unwrapOffsets ) {
121 fragment.unwrapText( unwrapOffsets[ 0 ], unwrapOffsets[ 1 ] );
122 unwrapped = true;
123 }
124
125 const wrappedFragment = fragment.wrapText( before, after, placeholder );
126 if ( !unwrapped && wrappedFragment !== fragment ) {
127 if ( !ve.dm.LinearData.static.isElementData(
128 wrappedFragment.collapseToStart().adjustLinearSelection( -1, 0 ).getData()[ 0 ]
129 ) ) {
130 wrappedFragment.collapseToStart().insertContent( [ { type: '/paragraph' }, { type: 'paragraph' } ] );
131 }
132 if ( !ve.dm.LinearData.static.isElementData(
133 wrappedFragment.collapseToEnd().adjustLinearSelection( 0, 1 ).getData()[ 0 ]
134 ) ) {
135 wrappedFragment.collapseToEnd().insertContent( [ { type: '/paragraph' }, { type: 'paragraph' } ] );
136 }
137 }
138 }
139 originalFragment.select();
140 return true;
141 };
142
143 /* Registration */
144
145 ve.ui.actionFactory.register( ve.ui.MWWikitextAction );