-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnnote.js
More file actions
executable file
·4574 lines (4522 loc) · 135 KB
/
mnnote.js
File metadata and controls
executable file
·4574 lines (4522 loc) · 135 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
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
class MNNote{
static noteCache = {}
/**
*
* @param {string} message
* @param {any} detail
* @param {["INFO","ERROR","WARNING","DEBUG"]} level
*/
static log(message,detail,level = "INFO"){
if (typeof MNLog !== "undefined") {
MNLog.log({message:message,detail:detail,source:"MNNote",level:level})
return
}
MNUtil.log({message:message,detail:detail,source:"MNNote",level:level})
}
/** @type {MbBookNote} */
note
/**
* Initializes a new MNNote instance.
*
* This constructor initializes a new MNNote instance based on the provided note object. The note object can be of various types:
* - An MbBookNote instance.
* - A string representing a note URL.
* - A string representing a note ID.
* - A configuration object for creating a new note.
*
* If the note object is a string representing a note URL, the constructor will attempt to retrieve the corresponding note from the URL.
* If the note object is a string representing a note ID, the constructor will attempt to retrieve the corresponding note from the database.
* If the note object is a configuration object, the constructor will create a new note with the specified properties.
*
* @param {MbBookNote|string|object} note - The note object to initialize the MNNote instance with.
*/
constructor(note,type = undefined) {
try {
if (type === undefined) {
type = MNUtil.typeOf(note)
}
switch (type) {
case 'MbBookNote':
this.note = note
break;
case 'NoteURL':
let NoteFromURL = MNUtil.getNoteById(MNUtil.getNoteIdByURL(note))
if (NoteFromURL) {
this.note = NoteFromURL
}
case "NoteId":
case 'string':
let targetNoteId = note.trim()
let targetNote = MNUtil.getNoteById(targetNoteId)
if (targetNote) {
this.note = targetNote
}
// MNUtil.showHUD(this.note.noteId)
break;
// MNUtil.copyJSON(targetNoteId+":"+this.note.noteId)
case "NoteConfig":
let config = note
let notebook = MNUtil.currentNotebook
let title = config.title ?? ""
let content = config.excerptText ?? config.content ?? ""
let markdown = config.excerptTextMarkdown ?? config.markdown ?? false
// MNUtil.showHUD("new note")
// MNUtil.copyJSON(note)
this.note = Note.createWithTitleNotebookDocument(title, notebook, MNUtil.currentDocController.document)
if (content.trim()) {//excerptText参数优先级高于content
this.note.excerptText = content.trim()
if (markdown) {
this.note.excerptTextMarkdown = true
if (/!\[.*?\]\((data:image\/.*;base64,.*?)(\))/.test(config.excerptText)) {
this.note.processMarkdownBase64Images()
}
}
}
if (config.inCurrentChildMap) {
if (this.currentChildMap) {
let child = MNNote.currentChildMap.createChildNote(config)
return child
}
}
if (config.color !== undefined) {
this.note.colorIndex = MNUtil.getColorIndex(config.color)
}
if (config.colorIndex !== undefined) {
this.note.colorIndex = config.colorIndex
}
if ("tags" in config && config.tags.length) {
this.note.appendTextComment(config.tags.map(k => '#'+k.replace(/\s+/g, "_")).join(" "))
}
if ("html" in config ) {
note.appendHtmlComment(config.html, config.html, {width:1000,height:500}, "")
}
if ("parentNoteId" in config && config.parentNoteId) {
let parentNote = MNNote.new(config.parentNoteId)
if (parentNote) {
this.note = parentNote.addAsChildNote(this.note)
}
}
break;
default:
break;
}
} catch (error) {
MNNote.addErrorLog(error, "MNNote.constructor")
}
}
/**
* Creates a new MNNote instance based on the provided note object.
*
* This static method initializes a new MNNote instance based on the provided note object. The note object can be of various types:
* - An MbBookNote instance.
* - A string representing a note URL.
* - A string representing a note ID.
* - A configuration object for creating a new note.
*
* If the note object is a string representing a note URL, the method will attempt to retrieve the corresponding note from the URL.
* If the note object is a string representing a note ID, the method will attempt to retrieve the corresponding note from the database.
* If the note object is a configuration object, the method will create a new note with the specified properties.
*
* @param {MbBookNote|string|object} note - The note object to initialize the MNNote instance with.
* @returns {MNNote|undefined} The initialized MNNote instance or undefined if the note object is invalid.
*/
static new(note,alert = true){
if (note === undefined) {
return undefined
}
let noteId = undefined
let noteType = MNUtil.typeOf(note)
let newNote = undefined
switch (noteType) {
case "MNNote":
return note;//原地返回
case 'MbBookNote':
return this.noteFromNote(note)
case 'TocNoteURL':
case 'NoteURL':
noteId = MNUtil.getNoteIdByURL(note)
return this.noteFromId(noteId)
case "NoteId":
return MNNote.noteFromId(note)
case 'string':
if (note === "current" || note === "currentNote" || note === "focused" || note === "focusedNote") {
return MNNote.getFocusNote()
}
let targetNoteId = note.trim()
return MNNote.noteFromId(targetNoteId)
case "NoteConfig":
let config = note
if (!MNUtil.currentDocController.document) {
MNUtil.confirm("No document in studyset!", "学习集中没有文档!")
return undefined
}
newNote = new MNNote(config,"NoteConfig")
this.noteCache[newNote.noteId] = newNote
return newNote
default:
return undefined
}
}
/**
* 从缓存中获取note
* @param {string} noteId
* @returns {MNNote|undefined}
*/
static getNoteFromCache(noteId){
if (noteId in this.noteCache) {
return this.noteCache[noteId]
}
return undefined
}
/**
* 统一处理缓存问题,如果noteId在缓存中,则返回缓存中的note,否则创建新的note并缓存
* @param {string} noteId
* @returns {MNNote|undefined}
*/
static noteFromId(noteId){
try {
if (noteId in this.noteCache) {
let newNote = this.getNoteFromCache(noteId)
return newNote
}
let newNote = new MNNote(noteId,"NoteId")
this.noteCache[noteId] = newNote
return newNote
} catch (error) {
MNNote.addErrorLog(error, "noteFromId", noteId)
return undefined
}
}
/**
* 统一处理缓存问题,如果noteId在缓存中,则返回缓存中的note,否则创建新的note并缓存
* @param {MbBookNote} note
* @returns {MNNote|undefined}
*/
static noteFromNote(note){
let noteId = note.noteId
try {
if (noteId in this.noteCache) {
let newNote = this.getNoteFromCache(noteId)
return newNote
}
let newNote = new MNNote(note,"MbBookNote")
this.noteCache[noteId] = newNote
return newNote
} catch (error) {
MNNote.addErrorLog(error, "noteFromNote", noteId)
return undefined
}
}
get noteId() {
return this.note.noteId
}
get id(){
return this.note.noteId
}
/**
* 不考虑评论
* @returns {string}
*/
get type(){
let type = "textNote"
let isBlankNote = MNUtil.isBlankNote(this)
if (isBlankNote) {
type = "blankTextNote"
return type
}
if (this.excerptPic) {
if ("video" in this.excerptPic) {
type = "videoNote"
return type
}
if (!this.textFirst) {
type = "imageNote"
return type
}
}
return type
}
get isBlankNote(){
let excerptPic = this.note.excerptPic
if (excerptPic) {
let imageData = MNUtil.getMediaByHash(excerptPic.paint)
return MNUtil.isEmptyImage(imageData)
}
return false
}
get notebookId() {
return this.note.notebookId
}
get notebook(){
return MNNotebook.new(this.notebookId)
}
/**
* 文档摘录和它在脑图对应的卡片具有不同的id,通过originNoteId可以获得文档摘录的id
*
* @returns {string} The original note ID of the merged note.
*/
get originNoteId(){
return this.note.originNoteId
}
/**
* 文档摘录和它在脑图对应的卡片具有不同的id,通过originNoteId可以获得文档摘录的id
*
* @returns {MNNote} The original note ID of the merged note.
*/
get originNote(){
return MNNote.new(this.note.originNoteId)
}
/**
* Retrieves the note ID of the main note in a group of merged notes.
*
* This method returns the note ID of the main note in a group of merged notes. This is useful for identifying the primary note in a set of combined notes.
*
* @returns {string} The note ID of the main note in the group of merged notes.
*/
get groupNoteId(){
return this.note.groupNoteId
}
/**
* @returns {MNNote|undefined} The group note of the current note.
*/
get groupNote(){
if (this.note.groupNoteId) {
return MNNote.new(this.note.groupNoteId)
}
return undefined
}
get groupMode(){
return this.note.groupMode
}
/**
* @returns {string}
*/
get getNoteColorHex(){
return MNUtil.noteColorByNotebookIdAndColorIndex(this.notebookId,this.colorIndex)
// let colorIndex = this.note.colorIndex
// let theme = MNUtil.app.currentTheme
// let colorConfig = {
// Default:["#ffffb4","#ccfdc4","#b4d1fb","#f3aebe","#ffff54","#75fb4c","#55bbf9","#ea3323","#ef8733","#377e47","#173dac","#be3223","#ffffff","#dadada","#b4b4b4","#bd9edc"],
// Dark:["#a0a071","#809f7b","#71839e","#986d77","#a0a032","#479e2c","#33759c","#921c12","#96551c","#204f2c","#0c266c","#771e14","#a0a0a0","#898989","#717171","#77638a"],
// Gary:["#d2d294","#a8d1a1","#94accf","#c88f9d","#d2d244","#5fcf3d","#459acd","#c0281b","#c46f28","#2c683a","#12328e","#9c281c","#d2d2d2","#b4b4b4","#949494","#9c82b5"]
// }
// let colorHexes = (theme in colorConfig)?colorConfig[theme]:colorConfig["Default"]
// if (colorIndex !== undefined && colorIndex >= 0) {
// return colorHexes[colorIndex]
// }
// return "#ffffff"
}
/**
* @returns {string}
*/
get colorHex(){
return MNUtil.noteColorByNotebookIdAndColorIndex(this.notebookId,this.colorIndex)
}
/**
* @returns {boolean}
* true: open
* false: closed (对子脑图无效,依然为open)
*/
get mindmapBranchClose(){
return this.note.mindmapBranchClose
}
/**
* Retrieves the child notes of the current note.
*
* This method returns an array of MNNote instances representing the child notes of the current note. If the current note has no child notes, it returns an empty array.
*
* @returns {MNNote[]} An array of MNNote instances representing the child notes.
*/
get childNotes() {
return this.note.childNotes?.map(k => new MNNote(k)) ?? []
}
/**
* Retrieves the parent note of the current note.
*
* This method returns an MNNote instance representing the parent note of the current note. If the current note has no parent note, it returns undefined.
*
* @returns {MNNote|undefined} The parent note of the current note, or undefined if there is no parent note.
*/
get parentNote() {
return this.note.parentNote && new MNNote(this.note.parentNote)
}
get parentNoteId(){
return this.note.parentNote?.noteId
}
/**
*
* @param {MNNote|string|MbBookNote} note
*/
set parentNote(note){
let parentNote = MNNote.new(note)
parentNote.addAsChildNote(this)
}
/**
* Retrieves the URL of the current note.
*
* This method generates and returns the URL of the current note, which can be used to reference or share the note.
*
* @returns {string} The URL of the current note.
*/
get noteURL(){
return MNUtil.version.version+'app://note/'+this.note.noteId
}
/**
* Retrieves the URL of the current note.
*
* This method generates and returns the URL of the current note, which can be used to reference or share the note.
*
* @returns {string} The URL of the current note.
*/
get url(){
return MNUtil.version.version+'app://note/'+this.note.noteId
}
/**
* Retrieves the child mind map note of the current note.
*
* This method returns an MNNote instance representing the child mind map note of the current note. If the current note has no child mind map note, it returns undefined.
*
* @returns {MNNote|undefined} The child mind map note of the current note, or undefined if there is no child mind map note.
*/
get childMindMap(){
if (this.note.childMindMap) {
return MNNote.new(this.note.childMindMap)
}
return undefined
}
/**
*
* @returns {{descendant:MNNote[],treeIndex:number[][]}}
*/
get descendantNodes() {
const { childNotes } = this
if (!childNotes.length) {
return {
descendant: [],
treeIndex: []
}
} else {
/**
*
* @param {MNNote[]} nodes
* @param {number} level
* @param {number[]} lastIndex
* @param {{descendant:MNNote[],treeIndex:number[][]}} ret
* @returns
*/
function down(
nodes,
level = 0,
lastIndex = [],
ret = {
descendant: [],
treeIndex: []
}
) {
level++
nodes.forEach((node, index) => {
ret.descendant.push(node)
lastIndex = lastIndex.slice(0, level - 1)
lastIndex.push(index)
ret.treeIndex.push(lastIndex)
if (node.childNotes?.length) {
down(node.childNotes, level, lastIndex, ret)
}
})
return ret
}
return down(childNotes)
}
}
get ancestorNodes() {
/**
*
* @param {MNNote} node
* @param {MNNote[]} ancestorNodes
* @returns
*/
function up(node, ancestorNodes) {
if (node.note.parentNote) {
const parentNode = new MNNote(node.note.parentNote)
ancestorNodes = up(parentNode, [...ancestorNodes, parentNode])
}
return ancestorNodes
}
return up(this, [])
}
/**
* Retrieves the notes associated with the current note.
*
* This method returns an array of notes that are linked to the current note. It includes the current note itself and any notes that are linked through the comments.
*
* @returns {MNNote[]} An array of MNNote instances representing the notes associated with the current note.
*/
get notes() {
return this.note.comments.reduce(
(acc, cur) => {
cur.type == "LinkNote" && MNUtil.noteExists(cur.noteid) && acc.push(MNNote.new(cur.noteid))
return acc
},
[this]
)
}
/**
* Retrieves the titles associated with the current note.
*
* This method splits the note title by semicolons and returns an array of unique titles. If the note title is not defined, it returns an empty array.
*
* @returns {string[]} An array of unique titles associated with the current note.
*/
get titles() {
let titles = this.note.noteTitle?.split(/\s*[;;]\s*/) ?? []
if (titles.length > 0) {
return MNUtil.unique(titles, true)
}
return []
}
/**
* Sets the titles associated with the current note.
*
* This method sets the titles associated with the current note by joining the provided array of titles with semicolons. If the excerpt text of the note is the same as the note title, it updates both the note title and the excerpt text.
*
* @param {string[]} titles - The array of titles to set for the current note.
*/
set titles(titles) {
if (typeof titles === "string") {
//如果提供的不是数组,则与set title方法一致
this.note.noteTitle = titles
return
}
const newTitle = MNUtil.unique(titles, true).join("; ")
if (this.note.excerptText === this.note.noteTitle) {
this.note.noteTitle = newTitle
this.note.excerptText = newTitle
} else {
this.note.noteTitle = newTitle
}
}
/**
* 卡片标题,但不包括markdown标题
* @returns {string}
*/
get title() {
let titles = this.titlesWithoutMarkdownSource
if (!titles || titles.length === 0) {
return ""
}
return titles.join("; ")
}
/**
*
* @param {string} title
* @returns
*/
set title(title) {
this.note.noteTitle = title
}
/**
* 这里不去除markdown标题,保证兼容性
* @returns {string}
*/
get noteTitle() {
return this.note.noteTitle ?? ""
}
/**
*
* @param {string} title
* @returns
*/
set noteTitle(title) {
this.note.noteTitle = title
}
/**
* 当卡片的内容为markdown时,其markdown标题会被添加进卡片标题,格式为{{title}},这里需要将其提取出来
* @returns {string[]}
*/
get titlesFromMarkdown(){
try {
let titles = this.titles
if (!titles || titles.length === 0) {
return []
}
return titles.filter(t=>{
if (/{{.*}}/.test(t)) {
return true
}
return false
})
} catch (error) {
MNNote.addErrorLog(error, "titlesFromMarkdown", this.noteId)
return []
}
}
/**
* 当卡片的内容为markdown时,其markdown标题会被添加进卡片标题,格式为{{title}},这里需要将其删除
* @returns {string[]}
*/
get titlesWithoutMarkdownSource(){
try {
let titles = this.titles
if (!titles || titles.length === 0) {
return []
}
return titles.filter(t=>{
if (/{{.*}}/.test(t)) {
return false
}
return true
})
} catch (error) {
MNNote.addErrorLog(error, "titlesWithoutMarkdownSource", this.noteId)
return []
}
}
get isOCR() {
let excerpt = this.excerpt
if (excerpt.type === "text") {
return excerpt.textFirst
}
return false
}
get excerptType(){
try {
let excerptPic = this.note.excerptPic
if (excerptPic) {
let possibleBlankNote = false
if (excerptPic.selLst) {
excerptPic.selLst.map(item=>{
if (item.pageNo > 10000) {
possibleBlankNote = true
}
})
}
if (possibleBlankNote && this.isBlankNote) {
return "text"
}
if ("video" in excerptPic) {
if (excerptPic.video_ext === "mp3") {
return "audio"
}
return "video"
}
if (this.textFirst) {
return "text"
}
return "image"
}
return "text"
} catch (error) {
MNNote.addErrorLog(error, "excerptType", this.noteId)
return undefined
}
}
_excerptCache = undefined
getExcerptInfo(){
try {
let info = {}
let excerptPic = this.note.excerptPic
if (excerptPic) {
if (excerptPic.paint) {
info.imageHash = excerptPic.paint
}
if (excerptPic.size) {
let size = MNUtil.NSValue2CGSize(excerptPic.size)
info.size = size
}
let possibleBlankNote = false
if (excerptPic.selLst) {
let pageNos = []
let selList = excerptPic.selLst.map(item=>{
let rect = MNUtil.NSValue2CGRect(item.rect)
let imgRect = MNUtil.NSValue2CGRect(item.imgRect)
pageNos.push(item.pageNo)
if (item.pageNo > 10000) {
possibleBlankNote = true
}
return {
rect: rect,
imgRect: imgRect,
pageNo: item.pageNo,
rotation: item.rotation
}
})
info.selList = selList
info.pageNos = pageNos
}
if (possibleBlankNote && this.isBlankNote) {
info.type = "text"
info.text = this.note.excerptText
info.textFirst = false
info.isBlankNote = true
return info
}
if ("video" in excerptPic) {
if (excerptPic.video_ext === "mp3") {
info.type = "audio"
info.audioHash = excerptPic.video
info.audioType = excerptPic.video_ext
info.audioOffset = excerptPic.video_offset
return info
}
info.type = "video"
info.videoHash = excerptPic.video
info.videoType = excerptPic.video_ext
info.videoOffset = excerptPic.video_offset
return info
}
if (this.textFirst) {
info.type = "text"
info.text = this.note.excerptText
info.textFirst = true
return info
}
info.type = "image"
return info
}
info.type = "text"
info.text = this.note.excerptText
info.textFirst = false
return info
} catch (error) {
MNNote.addErrorLog(error, "excerpt", this.noteId)
return undefined
}
}
/**
* 获取卡片内容信息
* @returns {{type:string,text:string,textFirst:boolean,isBlankNote:boolean,videoHash:string,videoType:string,videoOffset:number,audioHash:string,audioType:string,audioOffset:number,imageHash:string,size:CGSize,selList:CGRect[],pageNos:number[]}}
*/
get excerpt(){
if (this._excerptCache) {
let refreshTime = this._excerptCache.refreshTime
if (refreshTime && (Date.now() - refreshTime < 100)) {
return this._excerptCache.info
}
}else{
this._excerptCache = {}
}
this._excerptCache.info = this.getExcerptInfo()
this._excerptCache.refreshTime = Date.now()
return this._excerptCache.info
}
get textFirst() {
let textFirst = this.note.textFirst
if (textFirst === undefined) {
textFirst = false
}
return textFirst
}
/**
* set textFirst
* @param {boolean} on
* @returns
*/
set textFirst(on){
this.note.textFirst = on
}
get excerptText(){
return this.note.excerptText
}
get excerptTextMarkdown(){
return this.note.excerptTextMarkdown;
}
set excerptTextMarkdown(status){
this.note.excerptTextMarkdown = status
}
set excerptText(text){
this.note.excerptText = text
if (this.excerptPic && !this.textFirst) {
this.textFirst = true
}
}
get mainExcerptText() {
return this.note.excerptText ?? ""
}
/**
*
* @param {string} text
* @returns
*/
set mainExcerptText(text) {
this.note.excerptText = text
}
get excerptPic(){
let excerpt = this.excerpt
let excerptPic = this.note.excerptPic
if (!excerptPic) {
return undefined
}
excerptPic.size = excerpt.size
excerptPic.selList = excerpt.selList
return excerptPic
}
get excerptPicData(){
let excerpt = this.excerpt
let imageData = MNUtil.getMediaByHash(excerpt.imageHash)
return imageData
}
get excerptVideoData(){
let excerpt = this.excerpt
return MNUtil.getMediaByHash(excerpt.videoHash)
}
get excerptAudioData(){
let excerpt = this.excerpt
return MNUtil.getMediaByHash(excerpt.audioHash)
}
get colorIndex(){
return this.note.colorIndex
}
set colorIndex(index){
this.note.colorIndex = index
}
/**
* @param {string} color
*/
set color(color){
let colors = ["LightYellow", "LightGreen", "LightBlue", "LightRed","Yellow", "Green", "Blue", "Red", "Orange", "DarkGreen","DarkBlue", "DeepRed", "White", "LightGray","DarkGray", "Purple"]
let index = colors.indexOf(color)
if (index === -1) {
return
}
this.note.colorIndex = index
return
}
get color(){
let index = this.colorIndex
let colors = ["LightYellow", "LightGreen", "LightBlue", "LightRed","Yellow", "Green", "Blue", "Red", "Orange", "DarkGreen","DarkBlue", "DeepRed", "White", "LightGray","DarkGray", "Purple"]
if (index === -1) {
return ""
}
return colors[index]
}
get fillIndex(){
return this.note.fillIndex
}
set fillIndex(index){
this.note.fillIndex = index
}
/**
* @returns {string} The date when the note was created.
*/
get createDate(){
return this.note.createDate
}
/**
* @returns {number} The date when the note was created.
*/
get createDateNumber(){
return Date.parse(this.note.createDate)
}
/**
* @returns {string} The date when the note was last modified.
*/
get modifiedDate(){
return this.note.modifiedDate
}
/**
* @returns {number} The date when the note was last modified.
*/
get modifiedDateNumber(){
return Date.parse(this.note.modifiedDate)
}
get linkedNotes(){
return this.note.linkedNotes
}
get summaryLinks(){
return this.note.summaryLinks
}
get mediaList(){
return this.note.mediaList
}
get image(){//第一个图片
let imageData = MNNote.getImageFromNote(this,true)
let image = imageData?UIImage.imageWithData(imageData):undefined
return image
}
get imageId(){
return MNNote.getImageIdFromNote(this,true)
}
get imageData(){
return MNNote.getImageFromNote(this,true)
}
get images(){//所有图片
let imageDatas = MNNote.getImagesFromNote(this)
let images = imageDatas?imageDatas.map(imageData=>UIImage.imageWithData(imageData)):undefined
return images
}
get imageDatas(){//所有图片
return MNNote.getImagesFromNote(this)
}
get hasVideo(){
if (this.excerptPic && "video" in this.excerptPic) {
return true
}
return false
}
get videoId(){
if (this.excerptPic && "video" in this.excerptPic) {
return this.excerptPic.video
}
return undefined
}
get videoData(){
if (this.excerptPic && "video" in this.excerptPic) {
return MNUtil.getMediaByHash(this.excerptPic.video)
}
return undefined
}
/**
*
* @returns {NoteComment[]}
*/
get comments(){
return this.note.comments
}
/**
*
* @returns {MNComment[]}
*/
get MNComments(){
return MNComment.from(this)
}
/**
* get all tags, without '#'
* @returns {string[]}
*/
get tags() {
try {
// MNUtil.showHUD("length: "+this.note.comments.length)
const tags = this.note.comments.reduce((acc, cur) => {
// MNUtil.showHUD("cur.type")
if (cur.type == "TextNote" && MNUtil._isTagComment_(cur)) {
acc.push(...cur.text.split(/\s+/).filter(k => k.startsWith("#")))
}
return acc
}, [])
return tags.map(k => k.slice(1))
} catch (error) {
MNUtil.showHUD(error)
return []
}
}
/**
* set tags, will remove all old tags
*
* @param {string[]} tags
* @returns
*/
set tags(tags) {
this.tidyupTags()
tags = MNUtil.unique(tags, true)
const lastComment = this.note.comments[this.note.comments.length - 1]
if (lastComment?.type == "TextNote" && lastComment.text.startsWith("#")) {
this.note.removeCommentByIndex(this.note.comments.length - 1)
}
this.appendTextComments(tags.map(k => '#'+k).join(" "))
}
/**
* @returns {{ocr:string[],html:string[],md:string[]}}
*/
get excerptsTextPic() {
return this.notes.reduce(
(acc, cur) => {
Object.entries(MNNote.getNoteExcerptTextPic(cur)).forEach(([k, v]) => {
if (k in acc) acc[k].push(...v)
})
return acc
},
{
ocr: [],
html: [],
md: []
}
)
}
/**
* @returns {{html:string[],md:string[]}}
*/
get commentsTextPic() {
return this.note.comments.reduce(
(acc, cur) => {
if (cur.type === "PaintNote") {
const imgs = MNNote.exportPic(cur)
if (imgs)
Object.entries(imgs).forEach(([k, v]) => {
if (k in acc) acc[k].push(v)
})
} else if (cur.type == "TextNote" || cur.type == "HtmlNote") {
const text = cur.text.trim()
if (text && !text.includes("marginnote3app") && !text.startsWith("#"))
Object.values(acc).map(k => k.push(text))
}
return acc
},
{
html: [],
md: []
}
)
}
/** @returns {string[]} */
get excerptsText() {
return this.notes.reduce((acc, note) => {
const text = note.excerptText?.trim()
if (text) {
switch (note.type) {
case "textNote":
case "blankTextNote":
acc.push(text)
break;
case "imageNote":
break;
default:
break;
}
}
return acc
}, [])
}
/**
* get all comment text
* @returns {string[]}
*/
get commentsText() {
return this.note.comments.reduce((acc, cur) => {
if (cur.type == "TextNote" || cur.type == "HtmlNote") {
const text = cur.text.trim()