-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathindex.html
147 lines (136 loc) · 3.53 KB
/
index.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<!DOCTYPE html>
<html>
<head>
<title>Wiz Editor Demo</title>
<link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<script src='https://cdn.jsdelivr.net/npm/[email protected]/client/src/index.js' charset="utf-8"></script>
</head>
<body>
<div id="editor"></div>
</body>
<script>
const {
Editor,
createEditorPromise,
assert,
BlockElement,
blockUtils,
containerUtils,
CommandItemData,
MenuItem,
domUtils,
getEditor,
AuthMessage,
OnProgress,
EditorOptions,
SelectionDetail,
EditorDoc,
} = window.WizEditor;
const AppId = '';
const user1 = {
avatarUrl: 'https://www.wiz.cn/wp-content/new-uploads/2285af20-4006-11eb-8f21-01eb48012b63.jpeg',
userId: 'test',
displayName: 'test',
};
// --------------------------- mention data ----------------
function createLoadDataMenuItem(block) {
const menuItem = MenuItem.createElement(document.documentElement, {
id: '',
text: 'Load data...',
});
const input = document.createElement('input');
input.type = 'file';
input.accept = 'application/json';
domUtils.addClass(input, 'menu-item-input');
//
menuItem.appendChild(input);
//
input.onchange = (event) => {
// eslint-disable-next-line @typescript-eslint/no-shadow
assert(block);
const editor = getEditor(block);
const container = containerUtils.getParentContainer(block);
const index = containerUtils.getBlockIndex(block);
if (!blockUtils.isEmptyTextBlock(block)) {
// eslint-disable-next-line no-param-reassign
block = editor.insertTextBlock(container, index + 1, '');
}
//
assert(event.target);
if (input.files && input.files.length > 0) {
const file = input.files[0];
const reader = new FileReader();
reader.readAsText(file);
reader.onload = () => {
const data = JSON.parse(reader.result);
// eslint-disable-next-line no-use-before-define
loadDocument(document.getElementById('editor'), '', data);
};
input.files = null;
input.value = '';
}
};
return menuItem;
}
function handleGetBlockCommand(editor, block, detail, type) {
if (type === 'menu') {
//
const loadDataMenuItem = createLoadDataMenuItem(block);
//
return [{
id: '',
text: 'Load data',
order: 200,
element: loadDataMenuItem,
onClick: () => {},
}];
}
return [];
}
function replaceUrl(docId) {
// eslint-disable-next-line @typescript-eslint/no-shadow
const now = window.location.href;
if (now.endsWith(docId)) return;
//
const newUrl = `${window.location.origin}${window.location.pathname}?id=${docId}`;
window.history.pushState({}, '', newUrl);
//
localStorage.setItem('lastDocId', docId);
}
function handleUploadResource(editor, file) {
return domUtils.fileToDataUrl(file);
}
//
async function loadDocument(element, docId, initLocalData) {
//
const auth = {
appId: AppId,
...user1,
permission: 'w',
docId,
token: '',
};
//
const options = {
local: true,
initLocalData,
serverUrl: '',
placeholder: 'Please enter document title',
// markdownOnly: true,
lineNumber: true,
titleInEditor: true,
hideComments: true,
callbacks: {
onGetBlockCommand: handleGetBlockCommand,
onUploadResource: handleUploadResource,
},
};
const editor = await createEditorPromise(element, options, auth);
assert(editor);
return editor;
}
loadDocument(document.getElementById('editor'), user1, '').then((editor) => {
replaceUrl(editor.auth.docId);
});
</script>
</html>