@@ -53,25 +53,29 @@ export class CoreEditorOfflineProvider {
5353 {
5454 name : 'drafttext' ,
5555 type : 'TEXT' ,
56- notNull : true
56+ notNull : true ,
5757 } ,
5858 {
5959 name : 'pageinstance' ,
6060 type : 'TEXT' ,
61- notNull : true
61+ notNull : true ,
6262 } ,
6363 {
6464 name : 'timecreated' ,
6565 type : 'INTEGER' ,
66- notNull : true
66+ notNull : true ,
6767 } ,
6868 {
6969 name : 'timemodified' ,
7070 type : 'INTEGER' ,
71- notNull : true
71+ notNull : true ,
72+ } ,
73+ {
74+ name : 'originalcontent' ,
75+ type : 'TEXT' ,
7276 } ,
7377 ] ,
74- primaryKeys : [ 'contextlevel' , 'contextinstanceid' , 'elementid' , 'extraparams' ]
78+ primaryKeys : [ 'contextlevel' , 'contextinstanceid' , 'elementid' , 'extraparams' ] ,
7579 } ,
7680 ] ,
7781 } ;
@@ -158,11 +162,12 @@ export class CoreEditorOfflineProvider {
158162 * @param elementId Element ID.
159163 * @param extraParams Object with extra params to identify the draft.
160164 * @param pageInstance Unique identifier to prevent storing data from several sources at the same time.
165+ * @param originalContent Original content of the editor.
161166 * @param siteId Site ID. If not defined, current site.
162- * @return Promise resolved with the draft text . Undefined if no draft stored.
167+ * @return Promise resolved with the draft data . Undefined if no draft stored.
163168 */
164169 async resumeDraft ( contextLevel : string , contextInstanceId : number , elementId : string , extraParams : { [ name : string ] : any } ,
165- pageInstance : string , siteId ?: string ) : Promise < string > {
170+ pageInstance : string , originalContent ?: string , siteId ?: string ) : Promise < CoreEditorDraft > {
166171
167172 try {
168173 // Check if there is a draft stored.
@@ -175,15 +180,21 @@ export class CoreEditorOfflineProvider {
175180 entry . pageinstance = pageInstance ;
176181 entry . timemodified = Date . now ( ) ;
177182
183+ if ( originalContent && entry . originalcontent != originalContent ) {
184+ entry . originalcontent = originalContent ;
185+ entry . drafttext = '' ; // "Discard" the draft.
186+ }
187+
178188 await db . insertRecord ( this . DRAFT_TABLE , entry ) ;
179189 } catch ( error ) {
180190 // Ignore errors saving the draft. It shouldn't happen.
181191 }
182192
183- return entry . drafttext ;
193+ return entry ;
184194 } catch ( error ) {
185195 // No draft stored. Store an empty draft to save the pageinstance.
186- await this . saveDraft ( contextLevel , contextInstanceId , elementId , extraParams , pageInstance , '' , siteId ) ;
196+ await this . saveDraft ( contextLevel , contextInstanceId , elementId , extraParams , pageInstance , '' , originalContent ,
197+ siteId ) ;
187198 }
188199 }
189200
@@ -196,11 +207,12 @@ export class CoreEditorOfflineProvider {
196207 * @param extraParams Object with extra params to identify the draft.
197208 * @param pageInstance Unique identifier to prevent storing data from several sources at the same time.
198209 * @param draftText The text to store.
210+ * @param originalContent Original content of the editor.
199211 * @param siteId Site ID. If not defined, current site.
200212 * @return Promise resolved when done.
201213 */
202214 async saveDraft ( contextLevel : string , contextInstanceId : number , elementId : string , extraParams : { [ name : string ] : any } ,
203- pageInstance : string , draftText : string , siteId ?: string ) : Promise < void > {
215+ pageInstance : string , draftText : string , originalContent ?: string , siteId ?: string ) : Promise < void > {
204216
205217 let timecreated = Date . now ( ) ;
206218 let entry : CoreEditorDraft ;
@@ -214,10 +226,17 @@ export class CoreEditorOfflineProvider {
214226 // No draft already stored.
215227 }
216228
217- if ( entry && entry . pageinstance != pageInstance ) {
218- this . logger . warning ( `Discarding draft because of pageinstance. Context '${ contextLevel } ' '${ contextInstanceId } ', ` +
219- `element '${ elementId } '` ) ;
220- throw null ;
229+ if ( entry ) {
230+ if ( entry . pageinstance != pageInstance ) {
231+ this . logger . warning ( `Discarding draft because of pageinstance. Context '${ contextLevel } ' '${ contextInstanceId } ', ` +
232+ `element '${ elementId } '` ) ;
233+ throw null ;
234+ }
235+
236+ if ( ! originalContent ) {
237+ // Original content not set, use the one in the entry.
238+ originalContent = entry . originalcontent ;
239+ }
221240 }
222241
223242 const db = await this . sitesProvider . getSiteDb ( siteId ) ;
@@ -228,6 +247,9 @@ export class CoreEditorOfflineProvider {
228247 data . pageinstance = pageInstance ;
229248 data . timecreated = timecreated ;
230249 data . timemodified = Date . now ( ) ;
250+ if ( originalContent ) {
251+ data . originalcontent = originalContent ;
252+ }
231253
232254 await db . insertRecord ( this . DRAFT_TABLE , data ) ;
233255 }
@@ -251,4 +273,5 @@ type CoreEditorDraft = CoreEditorDraftPrimaryData & {
251273 pageinstance ?: string ; // Unique identifier to prevent storing data from several sources at the same time.
252274 timecreated ?: number ; // Time created.
253275 timemodified ?: number ; // Time modified.
276+ originalcontent ?: string ; // Original content of the editor.
254277} ;
0 commit comments