Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/preinit/ve.init.mw.DiffLoader.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki DiffLoader.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /* global ve */
9
10 /**
11 * Diff loader.
12 *
13 * @class mw.libs.ve.diffLoader
14 * @singleton
15 * @hideconstructor
16 */
17 ( function () {
18 const revCache = {};
19
20 mw.libs.ve = mw.libs.ve || {};
21
22 mw.libs.ve.diffLoader = {
23 /**
24 * Get a ve.dm.Document model from a Parsoid response
25 *
26 * @param {Object} response Parsoid response from the VisualEditor API
27 * @param {string|null} section Section. Null for the whole document.
28 * @return {ve.dm.Document|null} Document, or null if an invalid response
29 */
30 getModelFromResponse: function ( response, section ) {
31 // This method is only called after actually loading these, see `parseDocumentModulePromise`
32 const targetClass = ve.init.mw.ArticleTarget,
33 data = response ? ( response.visualeditor || response.visualeditoredit ) : null;
34 if ( data && typeof data.content === 'string' ) {
35 const doc = targetClass.static.parseDocument( data.content, 'visual', section, section !== null );
36 mw.libs.ve.stripRestbaseIds( doc );
37 return targetClass.static.createModelFromDom( doc, 'visual' );
38 }
39 return null;
40 },
41
42 /**
43 * Fetch a specific revision from Parsoid as a DM document, and cache in memory
44 *
45 * @param {number} revId Revision ID
46 * @param {string} [pageName] Page name, defaults to wgRelevantPageName
47 * @param {string|null} [section=null] Section. Null for the whole document.
48 * @param {jQuery.Promise} [parseDocumentModulePromise] Promise which resolves when Target#parseDocument is available
49 * @return {jQuery.Promise} Promise which resolves with a document model
50 */
51 fetchRevision: function ( revId, pageName, section, parseDocumentModulePromise ) {
52 pageName = pageName || mw.config.get( 'wgRelevantPageName' );
53 parseDocumentModulePromise = parseDocumentModulePromise || $.Deferred().resolve().promise();
54 section = section !== undefined ? section : null;
55
56 const cacheKey = revId + ( section !== null ? '/' + section : '' );
57
58 revCache[ cacheKey ] = revCache[ cacheKey ] ||
59 mw.libs.ve.targetLoader.requestParsoidData(
60 pageName,
61 { oldId: revId, targetName: 'diff' },
62 false,
63 // noMetadata, we only use `content` in getModelFromResponse
64 true
65 ).then(
66 ( response ) => parseDocumentModulePromise.then( () => mw.libs.ve.diffLoader.getModelFromResponse( response, section ) ),
67 () => {
68 // Clear promise. Do not cache errors.
69 delete revCache[ cacheKey ];
70 // Let caller handle the error code
71 return $.Deferred().rejectWith( this, arguments );
72 }
73 );
74
75 return revCache[ cacheKey ];
76 },
77
78 /**
79 * Get a visual diff generator promise
80 *
81 * @param {number|jQuery.Promise} oldIdOrPromise Old revision ID, or document model promise
82 * @param {number|jQuery.Promise} newIdOrPromise New revision ID, or document model promise
83 * @param {jQuery.Promise} [parseDocumentModulePromise] Promise which resolves when Target#parseDocument is available
84 * @param {string} [oldPageName] Old revision's page name, defaults to wgRelevantPageName
85 * @param {string} [newPageName] New revision's page name, defaults to oldPageName
86 * @return {jQuery.Promise} Promise which resolves with a ve.dm.VisualDiff generator function
87 */
88 getVisualDiffGeneratorPromise: function ( oldIdOrPromise, newIdOrPromise, parseDocumentModulePromise, oldPageName, newPageName ) {
89 parseDocumentModulePromise = parseDocumentModulePromise || $.Deferred().resolve().promise();
90 oldPageName = oldPageName || mw.config.get( 'wgRelevantPageName' );
91
92 const oldRevPromise = typeof oldIdOrPromise === 'number' ? this.fetchRevision( oldIdOrPromise, oldPageName, null, parseDocumentModulePromise ) : oldIdOrPromise;
93 const newRevPromise = typeof newIdOrPromise === 'number' ? this.fetchRevision( newIdOrPromise, newPageName, null, parseDocumentModulePromise ) : newIdOrPromise;
94
95 return $.when( oldRevPromise, newRevPromise, parseDocumentModulePromise ).then( ( oldDoc, newDoc ) => {
96 // TODO: Differ expects newDoc to be derived from oldDoc and contain all its store data.
97 // We may want to remove that assumption from the differ?
98 newDoc.getStore().merge( oldDoc.getStore() );
99 return () => new ve.dm.VisualDiff( oldDoc, newDoc );
100 } );
101 }
102
103 };
104 }() );