Skip to content

Commit ec8c1b8

Browse files
authored
[#69553] Remove jQuery from CKEditor build [FIXUP] (#108)
* Add Request.js as peer/dev dependency * Replace fetch() calls with Request.JS Request.JS provides a wrapper around `fetch`, taking care of setting the `X-CSRF-Token` header.
1 parent d04d5a8 commit ec8c1b8

File tree

7 files changed

+63
-36
lines changed

7 files changed

+63
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
- Removed jQuery dependency from the CKEditor build. All jQuery usage has been replaced with vanilla JavaScript equivalents:
88
- `jQuery.each()` replaced with native `Array.forEach()`
9-
- `jQuery.getJSON()` replaced with `fetch()` API
10-
- `jQuery.ajax()` replaced with `fetch()` API
9+
- `jQuery.getJSON()` replaced with Request.JS
10+
- `jQuery.ajax()` replaced with Request.JS
1111
- jQuery DOM manipulation replaced with native DOM APIs (`document.createElement()`, `element.parentElement`, `element.style`, etc.)
1212

1313
### Migration Notes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Now the webpack development mode is building the files and outputting them to `a
6262

6363
### jQuery Removal
6464

65-
As of version 11.2.0, this library no longer uses jQuery internally. All jQuery dependencies have been replaced with vanilla JavaScript equivalents using the Fetch API and native DOM manipulation.
65+
As of version 11.2.0, this library no longer uses jQuery internally. All jQuery dependencies have been replaced with vanilla JavaScript equivalents using Request.JS and native DOM manipulation.
6666

6767
**Important for downstream consumers (e.g., OpenProject):** While this library no longer uses jQuery internally, downstream applications should continue to expose the jQuery global if other parts of the application depend on it. Do not remove the jQuery global from the downstream application (OpenProject) yet until all components have been migrated.
6868

package-lock.json

Lines changed: 39 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@ckeditor/ckeditor5-watchdog": "44.3.0",
5252
"@ckeditor/ckeditor5-widget": "44.3.0",
5353
"@eslint/js": "^9.16.0",
54+
"@rails/request.js": "^0.0.13",
5455
"babel-jest": "^29.7.0",
5556
"css-loader": "^7.1.2",
5657
"eslint": "^9.23.0",
@@ -98,5 +99,8 @@
9899
},
99100
"dependencies": {
100101
"patch-package": "^8.0.0"
102+
},
103+
"peerDependencies": {
104+
"@rails/request.js": "^0.0.13"
101105
}
102106
}

src/mentions/user-mentions.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
getOPPath,
44
getPluginContext,
55
} from "../plugins/op-context/op-context";
6+
import { get } from '@rails/request.js';
67

78
export function userMentions(queryText) {
89
const editor = this;
@@ -24,18 +25,13 @@ export function userMentions(queryText) {
2425
return [];
2526
}
2627

27-
const url = getOPPath(editor).api.v3.principals(resource, queryText) + '&select=elements/_type,elements/id,elements/name';
28+
const url = getOPPath(editor).api.v3.principals(resource, queryText);
2829
const pluginContext = getPluginContext(editor);
2930
const base = window.OpenProject.urlRoot;
3031

3132
return new Promise((resolve, reject) => {
32-
fetch(url)
33-
.then(response => {
34-
if (!response.ok) {
35-
throw new Error(`HTTP error! status: ${response.status}`);
36-
}
37-
return response.json();
38-
})
33+
get(url, { responseKind: 'json', query: { select: 'elements/_type,elements/id,elements/name' } })
34+
.then(response => response.json)
3935
.then(collection => {
4036
resolve(_.uniqBy(collection._embedded.elements, (el) => el.id).map(mention => {
4137
const type = mention._type.toLowerCase();

src/mentions/work-package-mentions.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { get } from '@rails/request.js';
2+
13
export function workPackageMentions(prefix) {
24
return function (query) {
35
let editor = this;
@@ -9,14 +11,8 @@ export function workPackageMentions(prefix) {
911
}
1012

1113
return new Promise((resolve, reject) => {
12-
const params = new URLSearchParams({ q: query, scope: "all" });
13-
fetch(`${url}?${params.toString()}`)
14-
.then(response => {
15-
if (!response.ok) {
16-
throw new Error(`HTTP error! status: ${response.status}`);
17-
}
18-
return response.json();
19-
})
14+
get(url, { responseKind: 'json', query: { q: query, scope: "all" } })
15+
.then(response => response.json)
2016
.then(collection => {
2117
resolve(collection.map(wp => {
2218
const id = `${prefix}${wp.id}`;

src/plugins/op-preview.plugin.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ButtonView } from '@ckeditor/ckeditor5-ui';
77
import { Plugin } from '@ckeditor/ckeditor5-core';
88
import {getOPPath, getOPPreviewContext, getOPService} from './op-context/op-context';
99
import {enableItems, disableItems} from '../helpers/button-disabler';
10+
import { post } from '@rails/request.js';
1011

1112
export default class OPPreviewPlugin extends Plugin {
1213

@@ -39,7 +40,7 @@ export default class OPPreviewPlugin extends Plugin {
3940

4041
const previewWrapper = document.createElement('div');
4142
previewWrapper.className = 'ck-editor__preview op-uc-container';
42-
43+
4344
// Remove existing preview elements (only direct siblings)
4445
const existingPreviews = Array.from(reference.parentElement.children)
4546
.filter(el => el !== reference && el.classList.contains('ck-editor__preview'));
@@ -58,19 +59,12 @@ export default class OPPreviewPlugin extends Plugin {
5859
let link = getOPPreviewContext(editor);
5960
let url = getOPPath(editor).api.v3.previewMarkup(link);
6061

61-
fetch(url, {
62-
method: 'POST',
63-
headers: {
64-
'Content-Type': 'text/plain; charset=UTF-8'
65-
},
66-
body: editor.getData()
62+
post(url, {
63+
contentType: 'text/plain; charset=UTF-8',
64+
responseKind: 'html',
65+
body: editor.getData(),
6766
})
68-
.then(response => {
69-
if (!response.ok) {
70-
throw new Error(`HTTP error! status: ${response.status}`);
71-
}
72-
return response.text();
73-
})
67+
.then(response => response.html)
7468
.then(showPreview)
7569
.catch(error => {
7670
console.error('Error fetching preview:', error);
@@ -90,12 +84,12 @@ export default class OPPreviewPlugin extends Plugin {
9084
if (unregisterPreview) {
9185
unregisterPreview();
9286
}
93-
87+
9488
// Remove existing preview elements (only direct siblings)
9589
const existingPreviews = Array.from(mainEditor.parentElement.children)
9690
.filter(el => el !== mainEditor && el.classList.contains('ck-editor__preview'));
9791
existingPreviews.forEach(el => el.remove());
98-
92+
9993
mainEditor.style.display = '';
10094

10195
enableItems(editor);

0 commit comments

Comments
 (0)