Skip to content

Commit e877b06

Browse files
authored
Merge pull request #226 from CoreMedia/venus/CKE-56-pendingActions
CKE-56 pendingActions (main)
2 parents 60b7162 + f66f01f commit e877b06

File tree

3 files changed

+47
-45
lines changed

3 files changed

+47
-45
lines changed

packages/ckeditor5-coremedia-content-clipboard/src/ContentClipboardEditing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class ContentClipboardEditing extends Plugin {
5454

5555
#onAddMarker(editor: Editor) {
5656
return addContentMarkerConversion(this.#pendingMarkerNames, (markerData: MarkerData): void => {
57-
DataToModelMechanism.triggerLoadAndWriteToModel(editor, this.#pendingMarkerNames, markerData);
57+
void DataToModelMechanism.triggerLoadAndWriteToModel(editor, this.#pendingMarkerNames, markerData);
5858
});
5959
}
6060

packages/ckeditor5-coremedia-content-clipboard/src/DataToModelMechanism.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { ContentClipboardMarkerDataUtils, MarkerData } from "./ContentClipboardMarkerDataUtils";
22
import ContentInputDataCache, { ContentInputData } from "./ContentInputDataCache";
33
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";
55
import { Logger, LoggerProvider } from "@coremedia/ckeditor5-logging";
66
import MarkerRepositionUtil from "./MarkerRepositionUtil";
77
import {
88
ContentImportService,
9-
ContentReferenceResponse,
109
COREMEDIA_CONTEXT_KEY,
1110
createContentImportServiceDescriptor,
1211
createContentReferenceServiceDescriptor,
@@ -68,7 +67,11 @@ export default class DataToModelMechanism {
6867
* @param pendingMarkerNames - all markers that are not yet finally inserted.
6968
* @param markerData - object that holds information about the marker and the associated content insertion
7069
*/
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> {
7275
const logger = DataToModelMechanism.#logger;
7376
const markerName: string = ContentClipboardMarkerDataUtils.toMarkerName(
7477
markerData.insertionId,
@@ -91,49 +94,47 @@ export default class DataToModelMechanism {
9194
// images. The only two attributes to distinguish contents are linkable and
9295
// embeddable. Lookup an extender with the object type, call the `create`
9396
// 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) {
115116
const contextUriPath = editor.config.get(`${COREMEDIA_CONTEXT_KEY}.uriPath`);
116-
const importedContentReference = await contentImportService.import(response.request, {
117+
contentUri = await contentImportService.import(contentReferenceResponse.request, {
117118
contextUriPath: typeof contextUriPath === "string" ? contextUriPath : undefined,
118119
});
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+
}
137138
}
138139

139140
/**

pnpm-workspace.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ packages:
22
- packages/*
33
- itest
44
- app
5-
5+
onlyBuiltDependencies:
6+
- esbuild
67
patchedDependencies:
78
ckeditor5: patches/ckeditor5.patch

0 commit comments

Comments
 (0)