-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Index.vue
1059 lines (1000 loc) · 29.9 KB
/
Index.vue
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<template>
<div>
<el-dialog
class="sdialog"
width="98%"
:visible.sync="dialogVisible"
:show-close="false"
top="2vh"
:append-to-body="true"
:close-on-press-escape="false"
:close-on-click-modal="false"
>
<div slot="title" class="title-header pt-4">
<div class="inline-block">
<el-button
class="close-btn"
type="text"
icon="el-icon-close"
@click="goback"
></el-button>
<el-tooltip effect="dark" :content="$t('click_to_edit_page_title')">
<span @click="editTitle" class="page-title">{{ title }}</span>
</el-tooltip>
<el-tooltip effect="dark" :content="$t('select_catalog')">
<span
@click="showSelectCat = true"
class="cat-class v3-color-aux v3-font-size-sm"
>
<i class="el-icon-folder-opened mr-1"></i>{{ currentCatName }}
</span>
</el-tooltip>
</div>
<div class="inline-block float-right">
<el-button class="mr-2" type size="medium" @click="showNotify"
><i class="el-icon-s-comment mr-2"></i
>{{ $t('save_and_notify') }}</el-button
>
<el-dropdown
@command="dropdownCallback"
split-button
type="primary"
size="medium"
trigger="click"
title="Ctrl + S"
@click="save"
>
<span id="save-page"
><i class="el-icon-s-shop mr-2"></i>{{ $t('save') }}</span
>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item @click.native="saveToTemplate">{{
$t('save_to_templ')
}}</el-dropdown-item>
<el-tooltip
effect="dark"
:content="$t('lock_edit_tips')"
placement="left"
>
<el-dropdown-item v-if="!isLock" @click.native="setLock">{{
$t('lock_edit')
}}</el-dropdown-item>
</el-tooltip>
<el-dropdown-item v-if="isLock" @click.native="unlock">{{
$t('cacel_lock')
}}</el-dropdown-item>
<!-- <el-dropdown-item>保存前添加注释</el-dropdown-item> -->
</el-dropdown-menu>
</el-dropdown>
</div>
</div>
<div @keydown.ctrl.83.prevent="save" @keydown.meta.83.prevent="save">
<el-row class="fun-btn-group">
<el-dropdown type class="" size="medium" trigger="hover">
<el-button>
<i class="mr-1 fas fa-gear"></i>
{{ $t('doc_tool') }}
<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item @click.native="showHistoryVersionDialog = true">
<i class="mr-1 far fa-rectangle-history"></i>
{{ $t('history_version') }}</el-dropdown-item
>
<el-dropdown-item @click.native="showAttachment">
<el-badge :value="attachment_count">
<i class="mr-1 far fa-cloud-arrow-up"></i>
{{ $t('attachment') }}</el-badge
></el-dropdown-item
>
<el-dropdown-item @click.native="showSortPage">
<i class="mr-1 far fa-sort"></i>
{{ $t('sort_pages') }}</el-dropdown-item
>
<el-dropdown-item @click.native="showJsonToTableDialog = true">
<i class="mr-1 far fa-table"></i>
{{ $t('json_to_table') }}</el-dropdown-item
>
<el-dropdown-item @click.native="showJsonBeautifyDialog = true">
<i class="mr-1 far fa-goal-net"></i>
{{ $t('beautify_json') }}</el-dropdown-item
>
<el-dropdown-item @click.native="showPasteTableDialog = true">
<i class="mr-1 far fa-table"></i>
{{ $t('paste_insert_table') }}</el-dropdown-item
>
<el-dropdown-item
@click.native="showSqlToMarkdownTableDialog = true"
>
<i class="mr-1 far fa-table"></i>
{{ $t('sql_to_markdown_table') }}</el-dropdown-item
>
<el-dropdown-item
v-if="$lang == 'zh-cn'"
@click.native="showMock = true"
>
<i class="mr-1 far fa-database"></i>
Mock
</el-dropdown-item>
<el-dropdown-item
v-if="$lang == 'zh-cn'"
@click.native="showRunApi"
>
<i class="mr-1 far fa-link"></i>
{{ $t('http_test_api') }}</el-dropdown-item
>
</el-dropdown-menu>
</el-dropdown>
<el-button
v-if="$lang == 'zh-cn' && showAIBtn"
style="padding-top: 12px;padding-bottom: 12px;"
@click.native="showAI = true"
size="medium"
@click="createContent"
>
<i class="mr-1 far fa-robot"></i>
{{ $t('ai_assistant') }}</el-button
>
<el-dropdown type class="" size="medium" trigger="hover">
<el-button>
<i class="mr-1 fas fa-files"></i>
{{ $t('add_from_template')
}}<i class="el-icon-arrow-down el-icon--right"></i>
</el-button>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item @click.native="insertApiTemplate">
<i class="mr-1 far fa-plug"></i>
{{ $t('insert_apidoc_template') }}</el-dropdown-item
>
<el-dropdown-item @click.native="insertDatabaseTemplate">
<i class="mr-1 far fa-database"></i>
{{ $t('insert_database_doc_template') }}</el-dropdown-item
>
<el-dropdown-item @click.native="showTemplateDialog = true">
<i class="mr-1 far fa-files"></i>
{{ $t('more_templ') }}</el-dropdown-item
>
</el-dropdown-menu>
</el-dropdown>
</el-row>
<Editormd
v-bind:content="content"
v-if="showContent"
ref="Editormd"
type="editor"
id="page-editor"
></Editormd>
</div>
<div slot="footer" class="dialog-footer"></div>
</el-dialog>
<!-- 更多模板 -->
<TemplateList
v-if="showTemplateDialog"
:item_id="item_id"
:callback="
data => {
if (data && typeof data == 'string') {
insertValue(data)
}
showTemplateDialog = false
}
"
ref="TemplateList"
></TemplateList>
<!-- 历史版本 -->
<HistoryVersion
:is_show_recover_btn="true"
v-if="showHistoryVersionDialog"
:page_id="page_id"
:callback="
data => {
this.showHistoryVersionDialog = false
if (data && typeof data == 'string') {
insertValue(data)
}
}
"
></HistoryVersion>
<!-- Json转表格 组件 -->
<JsonToTable
v-if="showJsonToTableDialog"
:callback="
data => {
this.showJsonToTableDialog = false
if (data && typeof data == 'string') {
insertValue(data)
}
}
"
></JsonToTable>
<!-- Json格式化 -->
<JsonBeautify
v-if="showJsonBeautifyDialog"
:callback="
data => {
this.showJsonBeautifyDialog = false
if (data && typeof data == 'string') {
insertValue(data)
}
}
"
></JsonBeautify>
<!-- sql转表格 -->
<SqlToMarkdownTable
v-if="showSqlToMarkdownTableDialog"
:callback="
data => {
this.showSqlToMarkdownTableDialog = false
if (data && typeof data == 'string') {
insertValue(data)
}
}
"
></SqlToMarkdownTable>
<!-- 附件列表 -->
<AttachmentList
:item_id="item_id"
:manage="true"
:page_id="page_id"
v-if="showAttachmentListDialog"
:callback="
data => {
this.showAttachmentListDialog = false
if (data && typeof data == 'string') {
insertValue(data)
}
}
"
></AttachmentList>
<!-- 粘贴插入表格 -->
<PasteTable
v-if="showPasteTableDialog"
:callback="
data => {
this.showPasteTableDialog = false
if (data && typeof data == 'string') {
insertValue(data)
}
}
"
></PasteTable>
<!-- 页面排序 -->
<SortPage
v-if="sortPageVisiable"
:callback="
() => {
sortPageVisiable = false
}
"
:item_id="item_id"
:page_id="page_id"
:cat_id="cat_id"
ref="SortPage"
></SortPage>
<!-- 通知框 -->
<Notify
:page_id="page_id"
:item_id="item_id"
v-if="notifyVisiable"
:callback="
data => {
notifyVisiable = false
if (data && typeof data == 'string') {
is_notify = 1
notify_content = data
save(() => {
goback()
})
}
}
"
ref="Notify"
></Notify>
<SelectCat
:cat_id="cat_id"
:item_id="item_id"
v-if="showSelectCat"
:callback="
data => {
this.showSelectCat = false
if (data && (typeof data == 'string' || typeof data == 'number')) {
this.cat_id = data
refreshCat()
}
}
"
></SelectCat>
<Mock
:page_id="page_id"
:item_id="item_id"
v-if="showMock"
:callback="
() => {
showMock = false
}
"
></Mock>
<AI
:page_id="page_id"
:item_id="item_id"
v-if="showAI"
:callback="
data => {
this.showAI = false
if (data && typeof data == 'string') {
insertValue(data)
}
}
"
></AI>
<!-- 一个隐藏的可编辑元素。用于承载粘贴html代码。后续会用于转markdown -->
<div contenteditable="true" id="pastebin"></div>
</div>
</template>
<script>
import Editormd from '@/components/common/Editormd'
import JsonToTable from '@/components/common/JsonToTable'
import JsonBeautify from '@/components/common/JsonBeautify'
import SqlToMarkdownTable from '@/components/common/SqlToMarkdownTable'
import TemplateList from '@/components/page/edit/TemplateList'
import HistoryVersion from '@/components/page/edit/HistoryVersion'
import AttachmentList from '@/components/page/edit/AttachmentList'
import PasteTable from '@/components/page/edit/PasteTable'
import SortPage from '@/components/page/edit/SortPage'
import Notify from '@/components/page/edit/Notify'
import Mock from '@/components/page/edit/Mock'
import SelectCat from '@/components/catalog/Select'
import { Base64 } from 'js-base64'
import { rederPageContent } from '@/models/page'
import { getUserInfoFromStorage } from '@/models/user.js'
import AI from '@/components/page/edit/AI'
import {
apiTemplateZh,
databaseTemplateZh,
apiTemplateEn,
databaseTemplateEn
} from '@/models/template'
import TurndownService from 'turndown'
const turndownPluginGfm = require('turndown-plugin-gfm')
export default {
props: {
callback: () => {},
item_id: 0,
edit_page_id: 0,
copy_page_id: 0
},
data() {
return {
page_id: 0,
content: '',
title: '',
cat_id: '',
s_number: '',
attachment_count: '',
catalogs: [],
isLock: 0,
intervalId: 0,
saving: false,
sortPageVisiable: false,
notifyVisiable: false,
is_notify: 0,
notify_content: '',
showTemplateDialog: false,
showJsonToTableDialog: false,
showJsonBeautifyDialog: false,
showSqlToMarkdownTableDialog: false,
showHistoryVersionDialog: false,
showAttachmentListDialog: false,
showPasteTableDialog: false,
user_token: '',
dialogVisible: true,
showSelectCat: false,
currentCatName: '/',
showContent: false,
showMock: false,
showAI: false,
showAIBtn: false
}
},
components: {
Editormd,
JsonToTable,
JsonBeautify,
TemplateList,
HistoryVersion,
AttachmentList,
PasteTable,
SortPage,
Notify,
SqlToMarkdownTable,
SelectCat,
Mock,
AI
},
methods: {
// 获取页面内容
getPageContent(page_id) {
if (!page_id) {
page_id = this.page_id
}
this.request('/api/page/info', {
page_id: page_id
}).then(data => {
const json = data.data
this.content = rederPageContent(json.page_content)
this.showContent = true
setTimeout(() => {
// 如果长度大于4000,则关闭预览
if (this.content.length > 4000) {
this.editorUnwatch()
} else {
this.editorWatch()
}
// 开启草稿
this.draft()
}, 1000)
this.title = json.page_title
this.item_id = json.item_id
this.cat_id = json.cat_id
this.s_number = json.s_number
this.attachment_count =
json.attachment_count > 0 ? '...' : this.attachment_count
this.refreshCat()
})
},
// 获取所有目录
getCatalog(item_id) {
this.request('/api/catalog/catListName', {
item_id: item_id
}).then(data => {
var Info = data.data
Info.unshift({ cat_id: '0', cat_name: '/' })
this.catalogs = Info
if (this.catalogs && this.catalogs.length) {
for (let index = 0; index < this.catalogs.length; index++) {
const element = this.catalogs[index]
if (element.cat_id == this.cat_id) {
this.currentCatName = element.cat_name
return
}
}
}
this.currentCatName = '/'
})
},
// 插入数据到编辑器中。插入到光标处。如果参数is_cover为真,则清空后再插入(即覆盖)。
insertValue(value, is_cover) {
if (value) {
let childRef = this.$refs.Editormd // 获取子组件
if (is_cover) {
// 清空
childRef.clear()
childRef.insertValue(value)
childRef.setCursorToTop()
} else {
childRef.insertValue(value)
}
}
},
// 插入api模板
insertApiTemplate() {
if (this.$lang == 'zh-cn') {
this.insertValue(apiTemplateZh)
} else {
this.insertValue(apiTemplateEn)
}
},
// 插入数据字典模板
insertDatabaseTemplate() {
if (this.$lang == 'zh-cn') {
this.insertValue(databaseTemplateZh)
} else {
this.insertValue(databaseTemplateEn)
}
},
// 关闭预览
editorUnwatch() {
let childRef = this.$refs.Editormd // 获取子组件
childRef.editorUnwatch()
if (localStorage.getItem('page_id_unwatch_' + this.page_id)) {
} else {
this.$alert(
'检测到本页面内容比较多,showdoc暂时关闭了html实时预览功能,以防止过多内容造成页面卡顿。你可以在编辑栏中找到预览按钮进行手动打开。'
)
localStorage.setItem('page_id_unwatch_' + this.page_id, 1)
}
},
//
editorWatch() {
let childRef = this.$refs.Editormd // 获取子组件
childRef.editorWatch()
},
showRunApi() {
window.open('http://runapi.showdoc.cc/')
},
// 附件
showAttachment() {
this.showAttachmentListDialog = true
localStorage.setItem('attachment', '1')
if (this.attachment_count == 'new') {
this.attachment_count = ''
}
},
// 展示页面排序
showSortPage() {
this.save(() => {
this.sortPageVisiable = true
})
},
// 展示通知对话框
showNotify() {
if (this.page_id > 0) {
this.notifyVisiable = true
} else {
this.save(() => {
this.notifyVisiable = true
})
}
},
save(callback) {
var that = this
if (this.saving) {
return false
}
this.saving = true
var loading = that.$loading()
let childRef = this.$refs.Editormd
var content = childRef.getMarkdown()
var cat_id = this.cat_id
var item_id = that.item_id
var page_id = that.page_id
this.request(
'/api/page/save',
{
page_id: page_id,
item_id: item_id,
page_title: that.title,
page_content: encodeURIComponent(content),
is_urlencode: 1,
cat_id: cat_id,
is_notify: this.is_notify,
notify_content: this.notify_content
},
'post',
false
).then(data => {
loading.close()
that.saving = false
if (data.error_code === 0) {
if (typeof callback == 'function') {
callback()
} else {
that.$message({
showClose: true,
message: '保存成功',
type: 'success'
})
}
// 删除草稿
that.deleteDraft()
if (that.page_id <= 0) {
that.page_id = data.data.page_id
// 更改url
that.$router.replace({
path: '/' + that.item_id + '/' + that.page_id
})
}
} else if (data.error_code === 10250) {
// 删除草稿
that.deleteDraft()
that.$alert(
'内容已保存。检测到你的账户尚未绑定邮箱或者邮箱还没有经过验证。当忘记密码的时候,你将面临着<font color="red">丢失数据</font>的风险。<a href="/user/setting" target="_blank">点此绑定邮箱</a>,忘记密码时可通过邮箱重置密码',
'提示',
{
dangerouslyUseHTMLString: true
}
)
} else {
that.$alert(data.error_message)
}
})
// 设置一个最长关闭时间
setTimeout(() => {
loading.close()
that.saving = false
}, 10000)
},
goback() {
var url = '/' + this.item_id + '/' + this.page_id
if(this.page_id){
this.callback()
}else{
this.$router.push({
path: url
}).then(()=>{
this.callback()
})
}
},
// 另存为模板
saveToTemplate() {
var that = this
let childRef = this.$refs.Editormd
var content = childRef.getMarkdown()
this.$prompt(that.$t('save_templ_title'), ' ', {}).then(data => {
this.request('/api/template/save', {
template_title: data.value,
template_content: content
}).then(data => {
this.$alert(this.$t('save_templ_text'))
})
})
},
/** 监听剪切板 **/
// 以实现粘贴上传图片,html网页转markdown等
clipboardEvents(e) {
var that = this
// 如果当前鼠标的焦点不在编辑器内,则中止
if (
!document
.querySelector('#page-editor .CodeMirror-wrap')
.contains(document.activeElement)
) {
return
}
var url = DocConfig.server + '/api/page/uploadImg'
var clipboard = e.clipboardData
for (var i = 0, len = clipboard.items.length; i < len; i++) {
// 如果剪切板里的内容是图片
if (
clipboard.items[i].kind == 'file' ||
clipboard.items[i].type.indexOf('image') > -1
) {
var imageFile = clipboard.items[i].getAsFile()
var form = new FormData()
form.append('t', 'ajax-uploadpic')
form.append('editormd-image-file', imageFile)
form.append('user_token', that.user_token)
var loading = ''
var callback = function(type, data) {
type = type || 'before'
switch (type) {
// 开始上传
case 'before':
loading = that.$loading()
break
// 服务器返回错误
case 'error':
loading.close()
that.$alert('图片上传失败')
break
// 上传成功
case 'success':
loading.close()
if (data.success == 1) {
var value = '![](' + data.url + ')'
that.insertValue(value)
} else {
that.$alert(data.message)
}
break
}
}
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
data: form,
processData: false,
contentType: false,
beforeSend: function() {
// eslint-disable-next-line standard/no-callback-literal
callback('before')
},
error: function() {
// eslint-disable-next-line standard/no-callback-literal
callback('error')
},
success: function(data) {
// eslint-disable-next-line standard/no-callback-literal
callback('success', data)
}
})
e.preventDefault()
}
// 如果剪切板里的内容是html
else if (clipboard.items[i].type == 'text/html') {
e.preventDefault() // 阻止默认粘贴事件
clipboard.items[i].getAsString(htmlData => {
var pastebin = document.querySelector('#pastebin')
pastebin.innerHTML = htmlData
var text = pastebin.innerText // 利用插入html元素来获取其纯文本
// 如果其纯文本内容少于200字,那就当作是纯文本粘贴
if (text.length < 200) {
that.insertValue(text)
} else {
// 如果其纯文本内容多于200字,那就问要转markdown还是纯文本粘贴
that
.$confirm(that.$t('past_html_tips'), ' ', {
confirmButtonText: that.$t('past_html_markdown'),
cancelButtonText: that.$t('past_html_text')
})
.then(data => {
var turndownService = new TurndownService()
var gfm = turndownPluginGfm.gfm
var tables = turndownPluginGfm.tables
var strikethrough = turndownPluginGfm.strikethrough
turndownService.use([gfm, tables, strikethrough])
var markdown = turndownService.turndown(htmlData)
that.insertValue(markdown)
})
.catch(() => {
that.insertValue(text)
})
}
})
} else {
// 无动作。让默认粘贴事件做事。
}
}
},
// 草稿
draft() {
var that = this
var pkey = 'page_content_' + this.page_id
let childRef = this.$refs.Editormd
// 定时保存文本内容到localStorage
setInterval(() => {
var content = childRef.getMarkdown()
localStorage.setItem(pkey, content)
}, 30 * 1000)
// 检测是否有定时保存的内容
var page_content = localStorage.getItem(pkey)
if (
page_content &&
page_content.length > 0 &&
page_content != childRef.getMarkdown() &&
childRef.getMarkdown() &&
childRef.getMarkdown().length > 10
) {
localStorage.removeItem(pkey)
that
.$confirm(that.$t('draft_tips'), '', {
showClose: false
})
.then(() => {
that.insertValue(page_content, true)
localStorage.removeItem(pkey)
})
.catch(() => {
localStorage.removeItem(pkey)
})
}
},
// 遍历删除草稿
deleteDraft() {
for (var i = 0; i < localStorage.length; i++) {
var name = localStorage.key(i)
if (name.indexOf('page_content_') > -1) {
localStorage.removeItem(name)
}
}
},
// 锁定
setLock() {
if (this.page_id > 0) {
this.request('/api/page/setLock', {
page_id: this.page_id,
item_id: this.item_id
}).then(() => {
this.isLock = 1
})
}
},
// 解除锁定
unlock() {
if (!this.isLock) {
return // 本来处于未锁定中的话,不发起请求
}
this.request('/api/page/setLock', {
page_id: this.page_id,
item_id: this.item_id,
lock_to: 1000
}).then(() => {
this.isLock = 0
})
},
// 如果用户处于锁定状态的话,用心跳保持锁定
heartBeatLock() {
this.intervalId = setInterval(() => {
if (this.isLock) {
this.setLock()
}
}, 20 * 60 * 1000)
},
// 判断页面是否被锁定编辑
remoteIsLock() {
this.request('/api/page/isLock', {
page_id: this.page_id,
item_id: this.item_id
}).then(res => {
// 判断已经锁定了不
if (res.data.lock > 0) {
if (res.data.is_cur_user > 0) {
this.isLock = 1
} else {
this.$alert(this.$t('locking') + res.data.lock_username, '', {
showClose: false
}).then(() => {
this.goback()
})
}
} else if (res.data.exceed > 0 && this.page_id <= 0) {
this.$alert(
'该项目页面数量超出限制。项目创建者可以开通高级版以获取更多配额。<br>如果你是项目创建者,你可以<a href="/prices" target="_blank" >点此查看不同账户类型的额度限制差异</a>,也可以<a href="/order/index" target="_blank" >点此去升级账户类型</a>。<br>如果你现在不方便处理,你可以等会再自行回到项目列表页,点击右上角的用户中心去升级。',
{
dangerouslyUseHTMLString: true
}
).then(() => {
this.goback()
})
} else {
this.setLock() // 如果没有被别人锁定,则进编辑页面后自己锁定。
}
})
},
// 由于页面关闭事件无法直接发起异步的ajax请求,所以用浏览器的navigator.sendBeacon来实现
unLockOnClose() {
let user_token = this.user_token
let analyticsData = new URLSearchParams({
page_id: this.page_id,
item_id: this.item_id,
lock_to: 1000,
user_token: user_token
})
let url = DocConfig.server + '/api/page/setLock'
if ('sendBeacon' in navigator) {
navigator.sendBeacon(url, analyticsData)
} else {
var client = new XMLHttpRequest()
client.open('POST', url, false)
client.send(analyticsData)
}
},
// 刷新目录列表
refreshCat() {
this.getCatalog(this.item_id)
},
// 去新建目录
goToCat() {
let routeUrl = this.$router.resolve({
path: '/catalog/' + this.item_id
})
window.open(routeUrl.href, '_blank')
},
editTitle() {
let defaultVar = ''
if (this.page_id || this.copy_page_id) {
defaultVar = this.title
}
this.$prompt('', '', { inputValue: defaultVar }).then(data => {
this.title = data.value
})
},
isShowAI() {
// 判断是否应该展示AI助手按钮
this.request('/api/common/homePageSetting', {}).then(res => {
if (res.data && res.data.is_show_ai) {
this.showAIBtn = true
}
})
}
},
mounted() {
this.page_id = this.edit_page_id
if (this.copy_page_id > 0) {
this.getPageContent(this.copy_page_id)
} else if (this.page_id > 0) {
this.getPageContent(this.page_id)
} else {
this.content = '\n'
this.showContent = true
this.title = this.$t('default_title')
this.getCatalog(this.item_id)
}
this.heartBeatLock()
this.remoteIsLock()
/** 监听剪切板 **/
document.addEventListener('paste', this.clipboardEvents)
window.addEventListener('beforeunload', this.unLockOnClose)
let g_open_cat_id = this.$store.state.open_cat_id // 全局变量-当前打开的目录id
// 如果this.page_id无效,则可以判定用户是在新建页面
// 此时可以考虑把目录设置为用户当前打开的页面的所属目录
if (this.page_id <= 0 && g_open_cat_id > 0) {
this.cat_id = g_open_cat_id
}
const userInfo = getUserInfoFromStorage()
this.user_token = userInfo.user_token
this.isShowAI()
},
beforeDestroy() {
// 监听剪切板
document.removeEventListener('paste', this.clipboardEvents)
this.$message.closeAll()
clearInterval(this.intervalId)
this.unlock()
window.removeEventListener('beforeunload', this.unLockOnClose)
}
}
</script>
<style scoped>
.container-narrow {
margin: 0 auto;
max-width: 90%;
}
.masthead {
width: 100%;
margin-top: 5px;
}
.cat {
width: 200px;
}
.num {
width: 60px;
}