-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdownNotesBio.js
198 lines (165 loc) · 4.92 KB
/
markdownNotesBio.js
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
var markdownNotesBio = markdownNotesBio || (() => {
const version = '1.0.1';
const lastUpdate = 1578078196;
const schemaVersion = 1.0;
const CONFIG = {
apiStart: '<!-- API_markdown -->',
apiEnd: '<!-- API_markdown_end -->'
};
const REGEX = {
apiNote: /(<!--\sAPI_markdown\s-->)/g
};
var DATA = {
styles: {},
mdList: {}
};
const badParse = ( o ) => {
return _.isUndefined(o) || _.isNull(o) || _.isNull(o.mdCssTag);
};
const purgeAsCss = ( oid ) => {
//purge any [css] handouts changed to [md] handouts
delete DATA.styles[oid];
delete state.markdownNotesBio.data.styles[oid];
delete DATA.mdList[oid];
delete state.markdownNotesBio.data.mdList[oid];
};
const purgeAsMd = ( oid ) => {
//purce any [md] handouts changed to [css] handouts
if( !_.isNull(Object.keys(DATA.mdList)) ) {
Object.keys(DATA.mdList).forEach(function(each) {
if ( _.contains(DATA.mdList[each], oid) ) {
DATA.mdList[each] = _.without(DATA.mdList[each], oid);
}
});
state.markdownNotesBio.data.mdList = DATA.mdList;
}
};
const setMdState = ( oid, cssId ) => {
purgeAsCss(oid);
if ( _.has(DATA.mdList,cssId) && !_.contains(DATA.mdList[cssId],oid)) {
DATA.mdList[cssId].push(oid);
state.markdownNotesBio
.data.mdList[cssId]=_.clone(DATA.mdList[cssId]);
}
};
const setCssState = ( oid, styles ) => {
DATA.styles[oid] = styles;
state.markdownNotesBio.data.styles[oid] = styles;
purgeAsMd(oid);
if( !_.has(DATA.mdList, oid) ) {
DATA.mdList[oid] = [];
state.markdownNotesBio.data.mdList[oid] = [];
}
};
const processGmnote = (obj) => {
var type = obj.get('type');
var mdResults;
obj.get( 'gmnotes', (gmnotes) => {
mdResults = markdown.Parser({
text: gmnotes,
styles: DATA.styles,
objid: obj.id
});
if ( badParse(mdResults) ) {
purgeAsCss(obj.id);
purgeAsMd(obj.id);
return;
}
if ( mdResults.mdCssTag == '[md]' ) {
setMdState(obj.id, mdResults.cssId);
}
if ( mdResults.mdCssTag == '[css]' ) {
setCssState(obj.id, mdResults.styles);
if ( !_.isNull(DATA.mdList[obj.id]) ) {
DATA.mdList[obj.id].forEach(function(each) {
var o = getObj('handout', each) || getObj('character', each);
if (o) {
processGmnote(o);
}
});
}
}
obj.set((type === 'handout' ? 'notes' : 'bio'), mdResults.markdown);
});
};
const handleGmnotesChange = (obj) => {
processGmnote(obj);
};
const handleNotesBioChange = (obj) => {
obj.get((obj.get('type')==='handout' ? 'notes' : 'bio'), (notesBio) => {
if( notesBio.match(REGEX.apiNote) !== null ) {
return;
}
processGmnote(obj);
});
};
const handleHandoutDestroy = (obj) => {
var tempList;
purgeAsMd(obj.id);
if ( !_.isNull(DATA.mdList[obj.id]) ) {
tempList = _.clone(DATA.mdList[obj.id]);
purgeAsCss(obj.id);
if ( !_.isNull(tempList) && !_.isUndefined(tempList) ) {
tempList.forEach(function(each) {
var o = getObj('handout', each) || getObj('character', each);
if (o) {
processGmnote(o);
}
});
}
}
};
const registerEventHandlers = () => {
on('change:handout:notes', handleNotesBioChange);
on('change:character:bio', handleNotesBioChange);
on('change:handout:gmnotes', handleGmnotesChange);
on('change:character:gmnotes', handleGmnotesChange);
on('destroy:handout', handleHandoutDestroy);
on('destroy:character', handleHandoutDestroy);
};
const readyScript = () => {
var handouts = _.pluck( findObjs({type: 'handout'}), 'id');
var refreshList = [];
var sampleCss;
log('-=> markdownNotesBio v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');
if( ! _.has(state,'markdownNotesBio') || state.markdownNotesBio.version !== schemaVersion) {
log(' > Updating Schema to v'+schemaVersion+' <');
state.markdownNotesBio = {
version: schemaVersion,
data: {
styles: {},
mdList: {}
}
};
}
if (typeof markdown === 'undefined') {
log(" > Missing 'markdown.js'");
}
_.each( _.difference(Object.keys(state.markdownNotesBio.data.styles), handouts), (id) => {
delete state.markdownNotesBio.data.styles[id];
});
_.each( _.difference(Object.keys(state.markdownNotesBio.data.mdList), handouts ), (id) => {
refreshList = _.union(refreshList, state.markdownNotesBio.data.mdList[id]);
delete state.markdownNotesBio.data.mdList[id];
});
refreshList = _.intersection( refreshList, handouts).slice(0);
_.each( refreshList, (id) => {
let mdObj = getObj('handout', id) || getObj('character', id);
if ( mdObj ) {
handleGmnotesChange(mdObj);
}
});
_.each( Object.keys(state.markdownNotesBio.data.mdList), (id) => {
state.markdownNotesBio.data.mdList[id] = _.intersection( state.markdownNotesBio.data.mdList[id], handouts).slice(0);
});
DATA.styles = state.markdownNotesBio.data.styles;
DATA.mdList = state.markdownNotesBio.data.mdList;
};
on("ready",()=>{
readyScript();
registerEventHandlers();
});
return {
// Public interface here
};
})();