File: ve.ui.MWMagicLinkNodeInspector.js

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (190 lines) | stat: -rw-r--r-- 5,672 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*!
 * VisualEditor UserInterface MWMagicLinkNodeInspector class.
 *
 * @copyright See AUTHORS.txt
 * @license The MIT License (MIT); see LICENSE.txt
 */

/**
 * Inspector for editing MediaWiki magic links (RFC/ISBN/PMID).
 *
 * @class
 * @extends ve.ui.NodeInspector
 *
 * @constructor
 * @param {Object} [config] Configuration options
 */
ve.ui.MWMagicLinkNodeInspector = function VeUiMWMagicLinkNodeInspector() {
	// Parent constructor
	ve.ui.MWMagicLinkNodeInspector.super.apply( this, arguments );
};

/* Inheritance */

OO.inheritClass( ve.ui.MWMagicLinkNodeInspector, ve.ui.NodeInspector );

/* Static properties */

ve.ui.MWMagicLinkNodeInspector.static.name = 'linkMagicNode';

ve.ui.MWMagicLinkNodeInspector.static.title = null; // see #getSetupProcess

ve.ui.MWMagicLinkNodeInspector.static.modelClasses = [ ve.dm.MWMagicLinkNode ];

ve.ui.MWMagicLinkNodeInspector.static.actions = [
	...ve.ui.MWMagicLinkNodeInspector.super.static.actions,
	{
		action: 'convert',
		label: OO.ui.deferMsg( 'visualeditor-magiclinknodeinspector-convert-link' ),
		modes: [ 'edit' ]
	}
];

/* Methods */

/**
 * @inheritdoc
 */
ve.ui.MWMagicLinkNodeInspector.prototype.initialize = function () {
	// Parent method
	ve.ui.MWMagicLinkNodeInspector.super.prototype.initialize.call( this );

	// Properties
	this.targetInput = new OO.ui.TextInputWidget( {
		validate: this.validate.bind( this )
	} );
	this.targetInput.on( 'change', this.onChange.bind( this ) );

	// Initialization
	this.form.$element.append( this.targetInput.$element );
};

/**
 * Return true if the given string is a valid magic link of the
 * appropriate type.
 *
 * @private
 * @param {string} str String to validate
 * @return {boolean} String is valid
 */
ve.ui.MWMagicLinkNodeInspector.prototype.validate = function ( str ) {
	const node = this.getFragment().getSelectedNode();
	return node.constructor.static.validateContent( str, node.getMagicType() );
};

ve.ui.MWMagicLinkNodeInspector.prototype.onChange = function ( value ) {
	// Disable the unsafe action buttons if the input isn't valid
	const isValid = this.validate( value );
	this.actions.forEach( null, ( action ) => {
		if ( !action.hasFlag( 'safe' ) ) {
			action.setDisabled( !isValid );
		}
	} );
};

/**
 * @inheritdoc
 */
ve.ui.MWMagicLinkNodeInspector.prototype.getActionProcess = function ( action ) {
	if ( ( action === 'done' || action === 'convert' ) &&
		!this.validate( this.targetInput.getValue() ) ) {
		// Don't close dialog: input isn't valid.
		return new OO.ui.Process( 0 );
	}
	if ( action === 'convert' ) {
		return new OO.ui.Process( () => {
			this.close( { action: action } );
		} );
	}
	return ve.ui.MWMagicLinkNodeInspector.super.prototype.getActionProcess.call( this, action );
};

/**
 * @inheritdoc
 */
ve.ui.MWMagicLinkNodeInspector.prototype.getSetupProcess = function ( data ) {
	// Set the title based on the node type
	const fragment = data.fragment,
		node = fragment instanceof ve.dm.SurfaceFragment ?
			fragment.getSelectedNode() : null,
		type = node instanceof ve.dm.MWMagicLinkNode ?
			node.getMagicType() : null,
		msg = type ?
			'visualeditor-magiclinknodeinspector-title-' + type.toLowerCase() :
			null;

	data = ve.extendObject( {
		// The following messages are used here
		// * visualeditor-magiclinknodeinspector-title-isbn
		// * visualeditor-magiclinknodeinspector-title-pmid
		// * visualeditor-magiclinknodeinspector-title-rfc
		title: msg ? OO.ui.deferMsg( msg ) : null
	}, data );
	return ve.ui.MWMagicLinkNodeInspector.super.prototype.getSetupProcess.call( this, data )
		.next( () => {
			// Initialization
			this.targetInput.setValue(
				this.selectedNode ? this.selectedNode.getAttribute( 'content' ) : ''
			).setReadOnly( this.isReadOnly() );
		} );
};

/**
 * @inheritdoc
 */
ve.ui.MWMagicLinkNodeInspector.prototype.getReadyProcess = function ( data ) {
	return ve.ui.MWMagicLinkNodeInspector.super.prototype.getReadyProcess.call( this, data )
		.next( () => {
			this.targetInput.focus().select();
		} );
};

/**
 * @inheritdoc
 */
ve.ui.MWMagicLinkNodeInspector.prototype.getTeardownProcess = function ( data ) {
	data = data || {};
	return ve.ui.MWMagicLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data )
		.first( () => {
			const surfaceView = this.manager.getSurface().getView(),
				surfaceModel = this.getFragment().getSurface(),
				doc = surfaceModel.getDocument(),
				nodeRange = this.selectedNode.getOuterRange(),
				value = this.targetInput.getValue(),
				done = data.action === 'done',
				convert = data.action === 'convert',
				remove = data.action === 'remove' || ( done && !value );

			if ( remove ) {
				surfaceModel.change(
					ve.dm.TransactionBuilder.static.newFromRemoval( doc, nodeRange )
				);
			} else if ( convert ) {
				const annotation = ve.dm.MWMagicLinkNode.static.annotationFromContent(
					value
				);
				if ( annotation ) {
					const annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone();
					annotations.push( annotation );
					const content = value.split( '' );
					ve.dm.Document.static.addAnnotationsToData( content, annotations );
					surfaceModel.change(
						ve.dm.TransactionBuilder.static.newFromReplacement( doc, nodeRange, content )
					);
					setTimeout( () => {
						surfaceView.selectAnnotation( ( view ) => view.model instanceof ve.dm.LinkAnnotation );
					} );
				}
			} else if ( done && this.validate( value ) ) {
				surfaceModel.change(
					ve.dm.TransactionBuilder.static.newFromAttributeChanges(
						doc, nodeRange.start, { content: value }
					)
				);
			}
		} );
};

/* Registration */

ve.ui.windowFactory.register( ve.ui.MWMagicLinkNodeInspector );