-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathform.html
More file actions
114 lines (99 loc) · 2.91 KB
/
form.html
File metadata and controls
114 lines (99 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CKEditor 5 with Div Example</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<script src="https://cdn.ckeditor.com/ckeditor5/39.0.0/classic/ckeditor.js"></script>
<style>
.ck-editor__editable_inline {
min-height: 400px !important;
height: 400px !important;
}
</style>
</head>
<body>
<div class="container">
<h1>CKEditor 5 + php upload 範例</h1>
<div class="input-group mb-1">
<div class="input-group-text">標題</div>
<input type="text" name="title" class="form-control">
</div>
<div id="editor">
<p>這裡是內容</p>
</div>
<div class="d-flex mt-1">
<div class="btn btn-sm btn-primary ms-auto btn-send">送出</div>
</div>
</div>
<script>
let editorInstance;
const btnSend = document.querySelector(".btn-send");
const saveURL = "./doAdd.php";
const inputTitle = document.querySelector("[name=title]")
btnSend.addEventListener("click", e => {
const formData = new FormData();
formData.append("title", inputTitle.value);
formData.append("content", editorInstance.getData());
fetch(saveURL, {
method: "POST",
body: formData
}).then(res=>res.json()).then(result=>{
console.log(result);
if(result.status === "success"){
window.location.href = "./index.php";
}else{
throw new Error(result.message);
}
}).catch(error => {
console.log(error);
alert(error.message)
});
})
class MyUploadAdapter {
constructor(loader) {
this.loader = loader;
}
upload() {
return this.loader.file
.then(file => new Promise((resolve, reject) => {
const data = new FormData();
data.append('upload', file);
fetch('upload.php', {
method: 'POST',
body: data
})
.then(response => response.json())
.then(data => {
resolve({
default: data.url
});
})
.catch(err => {
reject(err);
});
}));
}
abort() {
// If the user aborts the upload, this method is called.
}
}
function MyCustomUploadAdapterPlugin(editor) {
editor.plugins.get('FileRepository').createUploadAdapter = (loader) => {
return new MyUploadAdapter(loader);
};
}
ClassicEditor
.create(document.querySelector('#editor'), {
extraPlugins: [MyCustomUploadAdapterPlugin]
})
.then(editor => {
editorInstance = editor;
})
.catch(error => {
console.error(error);
});
</script>
</body>
</html>