concept G22Text > ConceptWithChangeListeners { settableWithVar S description; // the script (last "saved" version) settableWithVar S text; // text currently being modified in an editor (or null) // Note that "" doesn't count as null. settableWithVar S editingText; // When imported from another project settableWithVar S importNote; S myType() { ret dropPrefix("G22", shortClassName(this)); } toString { ret colonCombine(myType() + id, description); } void initEditingText { editingText(unnull(or(editingText, text))); } void receiveEditingText(S text) { //printVars_shorten("receiveEditingText", +text); editingText(text); } S stableText() { ret text; } S latestText() { ret or(editingText, text); } void completeEdit aka save() { S t = editingText; //printVars_shorten("completeEdit", +t); if (t != null) { setTextWithHistory(t); //printVars_shorten("completeEdit", +editingText); } } void setTextWithHistory(S text) { if (eq(this.text, text)) ret; text(text); saveTextToHistory(); } void discardEdit { S text = editedText(); if (text == null) ret; saveFieldToHistory("discardingEdit", text); editingText(null); } S textForEditing() { initEditingText(); ret editingText; } File historyFile() { ret fileInConceptsDir("History/" + shortDynName(this) + id + ".history"); } void saveTextToHistory() { saveFieldToHistory(+text); } void saveFieldToHistory(S field, S value) { File historyFile = historyFile(); if (historyFile == null) ret; S contents = value == null ? " empty" : " (" + nLines(value) + ", " + nChars(value) + "):\n" + indentx(value) + "\n" + "\n"; appendToTextFile(historyFile, "\n===\n" + "Concept ID: " + id + "\n" + "Date: " + dateWithMSUTC() + "\n" + firstToUpper(field) + contents + "===" + "\n"); } bool isSaved() { ret text != null; } bool isEditing() { ret editedText() != null; } S editedText() { ret eq(editingText, text) ? null : editingText; } G22Utils g22utils() { ret assertNotNull(g22utils := main g22utils(_concepts())); } Set allTexts() { ret asSet(llNonNulls(text(), editedText()); } }