1
1
import { ContentClipboardMarkerDataUtils , MarkerData } from "./ContentClipboardMarkerDataUtils" ;
2
2
import ContentInputDataCache , { ContentInputData } from "./ContentInputDataCache" ;
3
3
import { serviceAgent } from "@coremedia/service-agent" ;
4
- import { Editor , Node , Position , Range , Writer } from "ckeditor5" ;
4
+ import { Editor , Node , PendingActions , Position , Range , Writer } from "ckeditor5" ;
5
5
import { Logger , LoggerProvider } from "@coremedia/ckeditor5-logging" ;
6
6
import MarkerRepositionUtil from "./MarkerRepositionUtil" ;
7
7
import {
8
8
ContentImportService ,
9
- ContentReferenceResponse ,
10
9
COREMEDIA_CONTEXT_KEY ,
11
10
createContentImportServiceDescriptor ,
12
11
createContentReferenceServiceDescriptor ,
@@ -68,7 +67,11 @@ export default class DataToModelMechanism {
68
67
* @param pendingMarkerNames - all markers that are not yet finally inserted.
69
68
* @param markerData - object that holds information about the marker and the associated content insertion
70
69
*/
71
- static triggerLoadAndWriteToModel ( editor : Editor , pendingMarkerNames : string [ ] , markerData : MarkerData ) : void {
70
+ static async triggerLoadAndWriteToModel (
71
+ editor : Editor ,
72
+ pendingMarkerNames : string [ ] ,
73
+ markerData : MarkerData ,
74
+ ) : Promise < void > {
72
75
const logger = DataToModelMechanism . #logger;
73
76
const markerName : string = ContentClipboardMarkerDataUtils . toMarkerName (
74
77
markerData . insertionId ,
@@ -91,49 +94,47 @@ export default class DataToModelMechanism {
91
94
// images. The only two attributes to distinguish contents are linkable and
92
95
// embeddable. Lookup an extender with the object type, call the `create`
93
96
// model stuff. Take a promise and execute writeItemToModel.
94
- const uri = contentInputData . itemContext . uri ;
95
- serviceAgent
96
- . fetchService < IContentReferenceService > ( createContentReferenceServiceDescriptor ( ) )
97
- . then ( ( service ) => service . getContentReference ( uri ) )
98
- . then ( async ( response : ContentReferenceResponse ) => {
99
- if ( response . contentUri ) {
100
- //The reference uri is a content uri
101
- return Promise . resolve ( response . contentUri ) ;
102
- }
103
- if ( ! response . externalUriInformation ) {
104
- return Promise . reject ( "No content found and uri is not importable." ) ;
105
- }
106
- const contentImportService = await serviceAgent . fetchService < ContentImportService > (
107
- createContentImportServiceDescriptor ( ) ,
108
- ) ;
109
- if ( response . externalUriInformation . contentUri ) {
110
- //The external content has been imported previously. A content representation already exists.
111
- return Promise . resolve ( response . externalUriInformation . contentUri ) ;
112
- }
113
-
114
- //Neither a content nor a content representation found. Let's create a content representation.
97
+ const pendingActionsPlugin = editor . plugins . get ( PendingActions ) ;
98
+ const pendingAction = pendingActionsPlugin ?. add ( "Loading data and maybe importing external Content." ) ;
99
+ try {
100
+ const uri = contentInputData . itemContext . uri ;
101
+ const contentReferenceService = await serviceAgent . fetchService < IContentReferenceService > (
102
+ createContentReferenceServiceDescriptor ( ) ,
103
+ ) ;
104
+ const contentReferenceResponse = await contentReferenceService . getContentReference ( uri ) ;
105
+ // The reference uri is a content uri
106
+ // or the external content has been imported previously. A content representation already exists.
107
+ let contentUri =
108
+ contentReferenceResponse . contentUri ?? contentReferenceResponse . externalUriInformation ?. contentUri ;
109
+ if ( ! contentUri && ! contentReferenceResponse . externalUriInformation ) {
110
+ throw new Error ( "No content found and uri is not importable." ) ;
111
+ }
112
+ const contentImportService = await serviceAgent . fetchService < ContentImportService > (
113
+ createContentImportServiceDescriptor ( ) ,
114
+ ) ;
115
+ if ( ! contentUri ) {
115
116
const contextUriPath = editor . config . get ( `${ COREMEDIA_CONTEXT_KEY } .uriPath` ) ;
116
- const importedContentReference = await contentImportService . import ( response . request , {
117
+ contentUri = await contentImportService . import ( contentReferenceResponse . request , {
117
118
contextUriPath : typeof contextUriPath === "string" ? contextUriPath : undefined ,
118
119
} ) ;
119
- return Promise . resolve ( importedContentReference ) ;
120
- } )
121
- . then ( async ( uri : string ) => {
122
- const type = await this . #getType ( uri ) ;
123
- const createItemFunction = await this . lookupCreateItemFunction ( type , uri ) ;
124
- return DataToModelMechanism . #writeItemToModel (
125
- editor ,
126
- pendingMarkerNames ,
127
- contentInputData ,
128
- markerData ,
129
- createItemFunction ,
130
- ) ;
131
- } )
132
- . catch ( ( reason ) => {
133
- DataToModelMechanism . #markerCleanup ( editor , pendingMarkerNames , markerData ) ;
134
- logger . error ( "Error occurred in promise" , reason ) ;
135
- } )
136
- . finally ( ( ) => DataToModelMechanism . #finishInsertion ( editor ) ) ;
120
+ }
121
+
122
+ const type = await this . #getType ( contentUri ) ;
123
+ const createItemFunction = await this . lookupCreateItemFunction ( type , contentUri ) ;
124
+ DataToModelMechanism . #writeItemToModel (
125
+ editor ,
126
+ pendingMarkerNames ,
127
+ contentInputData ,
128
+ markerData ,
129
+ createItemFunction ,
130
+ ) ;
131
+ } catch ( error : unknown ) {
132
+ DataToModelMechanism . #markerCleanup ( editor , pendingMarkerNames , markerData ) ;
133
+ logger . error ( "Error occurred in promise" , error ) ;
134
+ } finally {
135
+ DataToModelMechanism . #finishInsertion ( editor ) ;
136
+ pendingActionsPlugin ?. remove ( pendingAction ) ;
137
+ }
137
138
}
138
139
139
140
/**
0 commit comments