Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWLinkAction class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Link action.
9 *
10 * Opens either MWLinkAnnotationInspector or MWLinkNodeInspector depending on what is selected.
11 *
12 * @class
13 * @extends ve.ui.LinkAction
14 * @constructor
15 * @param {ve.ui.Surface} surface Surface to act on
16 */
17 ve.ui.MWLinkAction = function VeUiMWLinkAction( surface ) {
18 // Parent constructor
19 ve.ui.MWLinkAction.super.call( this, surface );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWLinkAction, ve.ui.LinkAction );
25
26 /* Static Properties */
27
28 ve.ui.MWLinkAction.static.methods = [
29 ...ve.ui.MWLinkAction.super.static.methods,
30 'open', 'autolinkMagicLink'
31 ];
32
33 /* Static methods */
34
35 /**
36 * Get a link annotation from specified link text
37 *
38 * This is a static version of the method that can be used in the converter.
39 *
40 * @static
41 * @param {string} linktext Link text
42 * @param {HTMLDocument} doc Document
43 * @return {ve.dm.MWExternalLinkAnnotation|ve.dm.MWInternalLinkAnnotation} The annotation to use
44 */
45 ve.ui.MWLinkAction.static.getLinkAnnotation = function ( linktext, doc ) {
46 const href = linktext;
47
48 // Is this a "magic link"?
49 if ( ve.dm.MWMagicLinkNode.static.validateContent( linktext ) ) {
50 return ve.dm.MWMagicLinkNode.static.annotationFromContent( linktext );
51 }
52 // Is this an internal link?
53 const targetData = mw.libs.ve.getTargetDataFromHref( href, doc );
54 if ( targetData.isInternal ) {
55 const title = mw.Title.newFromText( targetData.title );
56 return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title );
57 }
58 // It's an external link.
59 return new ve.dm.MWExternalLinkAnnotation( {
60 type: 'link/mwExternal',
61 attributes: { href: href }
62 } );
63 };
64
65 /* Methods */
66
67 /**
68 * Match the trailing punctuation set used for autolinks in wikitext.
69 * Closing parens are only stripped if open parens are missing from the
70 * candidate text, so that URLs with embedded matched parentheses (like
71 * wiki articles with disambiguation text) autolink nicely.
72 *
73 * @inheritdoc
74 */
75 ve.ui.MWLinkAction.prototype.getTrailingPunctuation = function ( candidate ) {
76 // This is:
77 // * the "trailing punctuation" character set from
78 // Parse.php::makeFreeExternalLink(): [,;.:!?] and sometimes [)]
79 // * extended with characters banned by EXT_LINK_URL_CLASS: []<>"
80 // * further extended with international close quotes: "'”’›»“‘‹«」』
81 // https://en.wikipedia.org/wiki/Quotation_mark
82
83 // We could unescape '\[' but better to keep it balanced with '\]'
84 /* eslint-disable no-useless-escape */
85 return /\(/.test( candidate ) ?
86 /[,;.:!?\[\]<>"'”’›»“‘‹«」』]+$/ :
87 /[,;.:!?\[\]<>"'”’›»“‘‹«」』)]+$/;
88 /* eslint-enable no-useless-escape */
89 };
90
91 /**
92 * @inheritdoc
93 * @return {ve.dm.MWExternalLinkAnnotation|ve.dm.MWInternalLinkAnnotation} The annotation to use
94 */
95 ve.ui.MWLinkAction.prototype.getLinkAnnotation = function ( linktext ) {
96 return this.constructor.static.getLinkAnnotation( linktext, this.surface.getModel().getDocument().getHtmlDocument() );
97 };
98
99 /**
100 * Autolink the selected RFC/PMID/ISBN, which may have trailing punctuation
101 * followed by whitespace.
102 *
103 * @see ve.ui.LinkAction#autolinkUrl
104 * @return {boolean}
105 * True if the selection is a valid RFC/PMID/ISBN and the autolink action
106 * was executed; otherwise false.
107 */
108 ve.ui.MWLinkAction.prototype.autolinkMagicLink = function () {
109 return this.autolink( ( linktext ) => ve.dm.MWMagicLinkNode.static.validateContent( linktext ), ( doc, range, linktext ) => {
110 const annotations = doc.data.getAnnotationsFromRange( range ),
111 data = new ve.dm.ElementLinearData( annotations.store, [
112 {
113 type: 'link/mwMagic',
114 attributes: {
115 content: linktext
116 }
117 },
118 {
119 type: '/link/mwMagic'
120 }
121 ] );
122 // Apply annotations which covered the range.
123 // Before we get here #autolink has guaranteed that the annotations
124 // do not contain any link annotations.
125 data.setAnnotationsAtOffset( 0, annotations );
126 return ve.dm.TransactionBuilder.static.newFromReplacement(
127 doc, range, data.getData()
128 );
129 } );
130 };
131
132 /**
133 * Open either the 'link' or 'linkNode' window, depending on what is selected.
134 *
135 * @return {boolean} Action was executed
136 */
137 ve.ui.MWLinkAction.prototype.open = function () {
138 const fragment = this.surface.getModel().getFragment(),
139 selectedNode = fragment.getSelectedNode();
140
141 let windowName = 'link';
142 if ( selectedNode instanceof ve.dm.MWNumberedExternalLinkNode ) {
143 windowName = 'linkNode';
144 } else if ( selectedNode instanceof ve.dm.MWMagicLinkNode ) {
145 windowName = 'linkMagicNode';
146 }
147 this.surface.executeWithSource( 'window', 'open', this.source, windowName );
148 return true;
149 };
150
151 /* Registration */
152
153 ve.ui.actionFactory.register( ve.ui.MWLinkAction );
154
155 ve.ui.commandRegistry.register(
156 new ve.ui.Command(
157 'autolinkMagicLink', ve.ui.MWLinkAction.static.name, 'autolinkMagicLink',
158 { supportedSelections: [ 'linear' ] }
159 )
160 );
161
162 // The regexps don't have to be precise; we'll validate the magic
163 // link in #autolinkMagicLink above.
164 ve.ui.sequenceRegistry.register(
165 new ve.ui.Sequence(
166 'autolinkMagicLinkIsbn10', 'autolinkMagicLink', /\bISBN\s+(?!97[89])([0-9][ -]?){9}[0-9Xx]$/, 0,
167 {
168 setSelection: true,
169 delayed: false,
170 checkOnPaste: true
171 }
172 )
173 );
174 ve.ui.sequenceRegistry.register(
175 new ve.ui.Sequence(
176 'autolinkMagicLinkIsbn13', 'autolinkMagicLink', /\bISBN\s+(97[89])[ -]?([0-9][ -]?){9}[0-9Xx]$/, 0,
177 {
178 setSelection: true,
179 delayed: false,
180 checkOnPaste: true
181 }
182 )
183 );
184 ve.ui.sequenceRegistry.register(
185 new ve.ui.Sequence(
186 'autolinkMagicLinkIsbn', 'autolinkMagicLink', /\bISBN\s+(97[89][ -]?)?([0-9][ -]?){9}[0-9Xx]$/, 0,
187 {
188 setSelection: true,
189 delayed: true,
190 checkOnPaste: true
191 }
192 )
193 );
194 ve.ui.sequenceRegistry.register(
195 new ve.ui.Sequence(
196 'autolinkMagicLinkRfcPmid', 'autolinkMagicLink', /\b(RFC|PMID)\s+[0-9]+$/, 0,
197 {
198 setSelection: true,
199 delayed: true,
200 checkOnPaste: true
201 }
202 )
203 );