Skip to content

Commit 509e734

Browse files
committed
Replace fetch() calls with Request.JS
Request.JS provides a wrapper around `fetch`, taking care of setting the `X-CSRF-Token` header.
1 parent 28a11cd commit 509e734

File tree

5 files changed

+16
-28
lines changed

5 files changed

+16
-28
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

src/mentions/user-mentions.js

Lines changed: 3 additions & 7 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;
@@ -29,13 +30,8 @@ export function userMentions(queryText) {
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)
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 & 7 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;
@@ -10,13 +12,8 @@ export function workPackageMentions(prefix) {
1012

1113
return new Promise((resolve, reject) => {
1214
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-
})
15+
get(`${url}?${params.toString()}`)
16+
.then(response => response.json)
2017
.then(collection => {
2118
resolve(collection.map(wp => {
2219
const id = `${prefix}${wp.id}`;

src/plugins/op-preview.plugin.js

Lines changed: 6 additions & 11 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,13 @@ 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',
62+
post(url, {
6363
headers: {
6464
'Content-Type': 'text/plain; charset=UTF-8'
6565
},
6666
body: editor.getData()
6767
})
68-
.then(response => {
69-
if (!response.ok) {
70-
throw new Error(`HTTP error! status: ${response.status}`);
71-
}
72-
return response.text();
73-
})
68+
.then(response => response.text)
7469
.then(showPreview)
7570
.catch(error => {
7671
console.error('Error fetching preview:', error);
@@ -90,12 +85,12 @@ export default class OPPreviewPlugin extends Plugin {
9085
if (unregisterPreview) {
9186
unregisterPreview();
9287
}
93-
88+
9489
// Remove existing preview elements (only direct siblings)
9590
const existingPreviews = Array.from(mainEditor.parentElement.children)
9691
.filter(el => el !== mainEditor && el.classList.contains('ck-editor__preview'));
9792
existingPreviews.forEach(el => el.remove());
98-
93+
9994
mainEditor.style.display = '';
10095

10196
enableItems(editor);

0 commit comments

Comments
 (0)