Debian Codegraph @

extensions/VisualEditor/editcheck/modules/AddReferenceEditCheck.js

raw symbols SCIP
1 mw.editcheck.AddReferenceEditCheck = function MWAddReferenceEditCheck( config ) {
2 // Parent constructor
3 mw.editcheck.AddReferenceEditCheck.super.call( this, config );
4 };
5
6 OO.inheritClass( mw.editcheck.AddReferenceEditCheck, mw.editcheck.BaseEditCheck );
7
8 mw.editcheck.AddReferenceEditCheck.static.name = 'addReference';
9
10 mw.editcheck.AddReferenceEditCheck.static.description = ve.msg( 'editcheck-dialog-addref-description' );
11
12 mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave = function ( surfaceModel ) {
13 return this.findAddedContent( surfaceModel.getDocument() ).map( ( range ) => {
14 const fragment = surfaceModel.getFragment( new ve.dm.LinearSelection( range ) );
15 return new mw.editcheck.EditCheckAction( {
16 highlight: fragment,
17 selection: this.adjustForPunctuation( fragment.collapseToEnd() ),
18 check: this
19 } );
20 } );
21 };
22
23 mw.editcheck.AddReferenceEditCheck.prototype.findAddedContent = function ( documentModel, includeReferencedContent ) {
24 // Broken out so a helper for tagging can call it
25 const ranges = this.getModifiedContentRanges( documentModel ).filter( ( range ) => {
26 if ( !includeReferencedContent ) {
27 // 4. Exclude any ranges that already contain references
28 for ( let i = range.start; i < range.end; i++ ) {
29 if ( documentModel.data.isElementData( i ) && documentModel.data.getType( i ) === 'mwReference' ) {
30 return false;
31 }
32 }
33 }
34 // 5. Exclude any ranges that aren't at the document root (i.e. image captions, table cells)
35 const branchNode = documentModel.getBranchNodeFromOffset( range.start );
36 if ( branchNode.getParent() !== documentModel.attachedRoot ) {
37 return false;
38 }
39 return true;
40 } );
41
42 return ranges;
43 };
44
45 mw.editcheck.AddReferenceEditCheck.prototype.act = function ( choice, action, contextItem ) {
46 // The complex citoid workflow means that we can't just count on a single "windowAction" here...
47 const windowAction = ve.ui.actionFactory.create( 'window', contextItem.context.getSurface(), 'check' );
48 switch ( choice ) {
49 case 'accept':
50 ve.track( 'activity.editCheckReferences', { action: 'edit-check-confirm' } );
51 action.selection.select();
52
53 return windowAction.open( 'citoid' ).then( ( instance ) => instance.closing ).then( ( citoidData ) => {
54 const citoidOrCiteDataDeferred = ve.createDeferred();
55 if ( citoidData && citoidData.action === 'manual-choose' ) {
56 // The plain reference dialog has been launched. Wait for the data from
57 // the basic Cite closing promise instead.
58 contextItem.context.getSurface().getDialogs().once( 'closing', ( win, closed, citeData ) => {
59 citoidOrCiteDataDeferred.resolve( citeData );
60 } );
61 } else {
62 // "Auto"/"re-use"/"close" means Citoid is finished and we can
63 // use the data form the Citoid closing promise.
64 citoidOrCiteDataDeferred.resolve( citoidData );
65 }
66 citoidOrCiteDataDeferred.promise().then( ( data ) => {
67 if ( !data ) {
68 // Reference was not inserted - re-open this context
69 setTimeout( () => {
70 // Deactivate again for mobile after teardown has modified selections
71 contextItem.context.getSurface().getView().deactivate();
72 contextItem.context.afterContextChange();
73 }, 500 );
74 } else {
75 // Edit check inspector is already closed by this point, but
76 // we need to end the workflow.
77 contextItem.close( citoidData );
78 }
79 } );
80 } );
81 case 'reject':
82 ve.track( 'activity.editCheckReferences', { action: 'edit-check-reject' } );
83 return windowAction.open(
84 'editCheckReferencesInspector',
85 {
86 fragment: action.highlight,
87 callback: contextItem.data.callback,
88 saveProcessDeferred: contextItem.data.saveProcessDeferred
89 }
90 // eslint-disable-next-line arrow-body-style
91 ).then( ( instance ) => {
92 // contextItem.openingCitoid = false;
93 return instance.closing;
94 } ).then( ( data ) => {
95 if ( !data ) {
96 // Form was closed, re-open this context
97 contextItem.context.afterContextChange();
98 } else {
99 contextItem.close( data );
100 }
101 } );
102 }
103 };
104
105 /**
106 * Adjust a fragment to include/exclude trailing punctuation
107 *
108 * @param {ve.dm.SurfaceFragment} insertionPointFragment
109 * @return {ve.dm.SurfaceFragment}
110 */
111 mw.editcheck.AddReferenceEditCheck.prototype.adjustForPunctuation = function ( insertionPointFragment ) {
112 if ( this.config.beforePunctuation ) {
113 // TODO: Use UnicodeJS properties directly once is https://gerrit.wikimedia.org/r/c/unicodejs/+/893832 merged
114 const sentenceProperties = {
115 ATerm: [ 0x002E, 0x2024, 0xFE52, 0xFF0E ],
116 STerm: [ 0x0021, 0x003F, 0x0589, 0x061E, 0x061F, 0x06D4, [ 0x0700, 0x0702 ], 0x07F9, 0x0837, 0x0839, 0x083D, 0x083E, 0x0964, 0x0965, 0x104A, 0x104B, 0x1362, 0x1367, 0x1368, 0x166E, 0x1735, 0x1736, 0x1803, 0x1809, 0x1944, 0x1945, [ 0x1AA8, 0x1AAB ], 0x1B5A, 0x1B5B, 0x1B5E, 0x1B5F, 0x1C3B, 0x1C3C, 0x1C7E, 0x1C7F, 0x203C, 0x203D, [ 0x2047, 0x2049 ], 0x2E2E, 0x2E3C, 0x3002, 0xA4FF, 0xA60E, 0xA60F, 0xA6F3, 0xA6F7, 0xA876, 0xA877, 0xA8CE, 0xA8CF, 0xA92F, 0xA9C8, 0xA9C9, [ 0xAA5D, 0xAA5F ], 0xAAF0, 0xAAF1, 0xABEB, 0xFE56, 0xFE57, 0xFF01, 0xFF1F, 0xFF61, 0x10A56, 0x10A57, [ 0x10F55, 0x10F59 ], 0x11047, 0x11048, [ 0x110BE, 0x110C1 ], [ 0x11141, 0x11143 ], 0x111C5, 0x111C6, 0x111CD, 0x111DE, 0x111DF, 0x11238, 0x11239, 0x1123B, 0x1123C, 0x112A9, 0x1144B, 0x1144C, 0x115C2, 0x115C3, [ 0x115C9, 0x115D7 ], 0x11641, 0x11642, [ 0x1173C, 0x1173E ], 0x11944, 0x11946, 0x11A42, 0x11A43, 0x11A9B, 0x11A9C, 0x11C41, 0x11C42, 0x11EF7, 0x11EF8, 0x16A6E, 0x16A6F, 0x16AF5, 0x16B37, 0x16B38, 0x16B44, 0x16E98, 0x1BC9F, 0x1DA88 ],
117 Close: [ 0x0022, [ 0x0027, 0x0029 ], 0x005B, 0x005D, 0x007B, 0x007D, 0x00AB, 0x00BB, [ 0x0F3A, 0x0F3D ], 0x169B, 0x169C, [ 0x2018, 0x201F ], 0x2039, 0x203A, 0x2045, 0x2046, 0x207D, 0x207E, 0x208D, 0x208E, [ 0x2308, 0x230B ], 0x2329, 0x232A, [ 0x275B, 0x2760 ], [ 0x2768, 0x2775 ], 0x27C5, 0x27C6, [ 0x27E6, 0x27EF ], [ 0x2983, 0x2998 ], [ 0x29D8, 0x29DB ], 0x29FC, 0x29FD, [ 0x2E00, 0x2E0D ], 0x2E1C, 0x2E1D, [ 0x2E20, 0x2E29 ], 0x2E42, [ 0x3008, 0x3011 ], [ 0x3014, 0x301B ], [ 0x301D, 0x301F ], 0xFD3E, 0xFD3F, 0xFE17, 0xFE18, [ 0xFE35, 0xFE44 ], 0xFE47, 0xFE48, [ 0xFE59, 0xFE5E ], 0xFF08, 0xFF09, 0xFF3B, 0xFF3D, 0xFF5B, 0xFF5D, 0xFF5F, 0xFF60, 0xFF62, 0xFF63, [ 0x1F676, 0x1F678 ] ],
118 SContinue: [ 0x002C, 0x002D, 0x003A, 0x055D, 0x060C, 0x060D, 0x07F8, 0x1802, 0x1808, 0x2013, 0x2014, 0x3001, 0xFE10, 0xFE11, 0xFE13, 0xFE31, 0xFE32, 0xFE50, 0xFE51, 0xFE55, 0xFE58, 0xFE63, 0xFF0C, 0xFF0D, 0xFF1A, 0xFF64 ]
119 };
120 const punctuationPattern = new RegExp(
121 unicodeJS.charRangeArrayRegexp( [].concat(
122 sentenceProperties.ATerm,
123 sentenceProperties.STerm,
124 sentenceProperties.Close,
125 sentenceProperties.SContinue
126 ) )
127 );
128 let lastCharacter = insertionPointFragment.adjustLinearSelection( -1, 0 ).getText();
129 while ( punctuationPattern.test( lastCharacter ) ) {
130 insertionPointFragment = insertionPointFragment.adjustLinearSelection( -1, -1 );
131 lastCharacter = insertionPointFragment.adjustLinearSelection( -1, 0 ).getText();
132 }
133 }
134 return insertionPointFragment;
135 };
136
137 mw.editcheck.editCheckFactory.register( mw.editcheck.AddReferenceEditCheck );