Skip to content

Commit

Permalink
feat: Update note Editor and model to relay on note editor extensions -
Browse files Browse the repository at this point in the history
  • Loading branch information
sofyenne committed Oct 15, 2024
1 parent 15ea72c commit f454009
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public class PageEntity implements Serializable {
private String lang;

private PagePropertiesEntity properties;

private boolean extensionDataUpdated;
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ public class Page {

private boolean hasChild;

private String latestVersionId;

private boolean isDeleted;

private Page parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ public Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity
note.setContent(note.getContent());
Page createdPage = createNote(noteBook, parentPage, note);
NotePageProperties properties = note.getProperties();
String draftPageId = String.valueOf(properties != null && properties.isDraft() ? properties.getNoteId() : null);
try {
if (properties != null) {
properties.setNoteId(Long.parseLong(createdPage.getId()));
Expand All @@ -273,6 +274,15 @@ public Page createNote(Wiki noteBook, String parentNoteName, Page note, Identity
createdPage.setCanImport(canImportNotes(note.getAuthor(), space, note));
createdPage.setCanView(canViewNotes(note.getAuthor(), space, note));
}
dataStorage.addPageVersion(createdPage, userIdentity.getUserId());
PageVersion pageVersion = dataStorage.getPublishedVersionByPageIdAndLang(Long.valueOf(createdPage.getId()), createdPage.getLang());
createdPage.setLatestVersionId(pageVersion != null ? pageVersion.getId() : null);
if (pageVersion != null && draftPageId != null) {
Map<String, String> eventData = new HashMap<>();
eventData.put("draftPageId", draftPageId);
eventData.put("pageVersionId", pageVersion.getId());
Utils.broadcast(listenerService, "note.page.version.created", this, eventData);
}
return createdPage;
} else {
throw new EntityNotFoundException("Parent note not found");
Expand Down Expand Up @@ -1006,6 +1016,8 @@ public List<PageHistory> getVersionsHistoryOfNote(Page note, String userName) th
@Override
public void createVersionOfNote(Page note, String userName) throws WikiException {
PageVersion pageVersion = dataStorage.addPageVersion(note, userName);
String pageVersionId = pageVersion.getId();
note.setLatestVersionId(pageVersionId);
if (note.getLang() != null) {
try {
NotePageProperties properties = note.getProperties();
Expand All @@ -1027,8 +1039,14 @@ public void createVersionOfNote(Page note, String userName) throws WikiException
null,
NOTE_METADATA_PAGE_OBJECT_TYPE,
NOTE_METADATA_VERSION_PAGE_OBJECT_TYPE,
userName
);
userName);
}
DraftPage draftPage = dataStorage.getLatestDraftPageByTargetPageAndLang(Long.valueOf(note.getId()), note.getLang());
if (draftPage != null) {
Map<String, String> eventData = new HashMap<>();
eventData.put("draftPageId", draftPage.getId());
eventData.put("pageVersionId", pageVersionId);
Utils.broadcast(listenerService, "note.page.version.created", this, eventData);
}
}

Expand Down Expand Up @@ -1211,6 +1229,14 @@ public DraftPage createDraftForExistPage(DraftPage draftPage,
log.error("Failed to save draft note metadata", e);
}
newDraftPage.setProperties(properties);
//
PageVersion pageVersion = getPublishedVersionByPageIdAndLang(Long.valueOf(newDraftPage.getTargetPageId()), newDraftPage.getLang());
if (pageVersion != null) {
Map<String, String> eventData = new HashMap<>();
eventData.put("pageVersionId", pageVersion.getId());
eventData.put("draftForExistingPageId", newDraftPage.getId());
Utils.broadcast(listenerService, "note.draft.for.exist.page.created", this, eventData);
}
return newDraftPage;
}

Expand Down Expand Up @@ -1405,6 +1431,7 @@ public Page getNoteByIdAndLang(Long pageId, Identity userIdentity, String source
page.setContent(publishedVersion.getContent());
page.setLang(publishedVersion.getLang());
page.setProperties(publishedVersion.getProperties());
page.setLatestVersionId(publishedVersion.getId());
if (lang != null) {
page.setMetadatas(retrieveMetadataItems(pageId + "-" + lang, userIdentity.getUserId()));
}
Expand All @@ -1421,6 +1448,7 @@ public Page getNoteByIdAndLang(Long pageId, String lang) {
page.setTitle(publishedVersion.getTitle());
page.setContent(publishedVersion.getContent());
page.setLang(publishedVersion.getLang());
page.setLatestVersionId(publishedVersion.getId());
}
return page;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,8 +824,15 @@ public Response updateNoteById(@Parameter(description = "Note id", required = tr
WikiPageParams noteParams = new WikiPageParams(note_.getWikiType(), note_.getWikiOwner(), newNoteName);
noteService.removeDraftOfNote(noteParams, note.getLang());
}
} else if (note_.isToBePublished()){
note_ = noteService.updateNote(note_, PageUpdateType.PUBLISH, identity);
} else if (note_.isToBePublished()) {
note_ = noteService.updateNote(note_, PageUpdateType.PUBLISH, identity);
} else if (note.isExtensionDataUpdated()) {
note_ = noteService.updateNote(note_, PageUpdateType.EDIT_PAGE_CONTENT_AND_TITLE, identity);
noteService.createVersionOfNote(note_, identity.getUserId());
if (!Utils.ANONYM_IDENTITY.equals(identity.getUserId())) {
WikiPageParams noteParams = new WikiPageParams(note_.getWikiType(), note_.getWikiOwner(), newNoteName);
noteService.removeDraftOfNote(noteParams, note.getLang());
}
} else {
// in this case, the note didnt change on title nor content. As we need the page
// url in front side, we compute it here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ public void testCreateDraftForNewPageWithProperties() throws Exception {
}

public void testCreateDraftForExistPageWithProperties() throws Exception {
Identity user = new Identity("user");
Identity user = new Identity("root");
this.bindMockedUploadService();
NotePageProperties notePageProperties = createNotePageProperties(0L, "alt text", "summary Test");
DraftPage draftPage = new DraftPage();
Expand Down Expand Up @@ -972,10 +972,10 @@ public void testFeaturedImageWhenRemoveDraftById() throws Exception {
}

public void testGetDraftsOfWiki() throws Exception {
Identity user = new Identity("user");
Identity root = new Identity("root");
Wiki portalWiki = getOrCreateWiki(wService, PortalConfig.PORTAL_TYPE, "testPortal");
Page note = noteService.createNote(portalWiki, "Home", new Page("testGetDraftsOfWiki", "testGetDraftsOfWiki"), user);
Page note2 = noteService.createNote(portalWiki, "Home", new Page("testGetDraftsOfWiki", "testGetDraftsOfWiki"), user);
Page note = noteService.createNote(portalWiki, "Home", new Page("testGetDraftsOfWiki", "testGetDraftsOfWiki"), root);
Page note2 = noteService.createNote(portalWiki, "Home", new Page("testGetDraftsOfWiki", "testGetDraftsOfWiki"), root);
DraftPage draftPage = new DraftPage();
draftPage.setTitle("test");
draftPage.setContent("test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CKEDITOR.editorConfig = function (config) {
'Table',
'EmbedSemantic',
'CodeSnippet',
'attachFile',
'InsertOptions'
];
if (webPageNote) {
Expand Down Expand Up @@ -68,7 +69,7 @@ CKEDITOR.editorConfig = function (config) {
},
{
name: 'blocks',
items: ['Blockquote']
items: ['Blockquote', 'attachFile']
},
];
let extraPlugins = `a11ychecker,balloonpanel,indent,indentblock,indentlist,codesnippet,sharedspace,copyformatting,table,tabletools,embedsemantic,autolink,colordialog${!webPageNote && ',tagSuggester' || ''},emoji,link,font,justify,widget,${!webPageNote && ',insertOptions' || ''},contextmenu,tabletools,tableresize,toc,linkBalloon,suggester`;
Expand All @@ -87,7 +88,7 @@ CKEDITOR.editorConfig = function (config) {
}
}
const notesEditorExtensions = extensionRegistry.loadExtensions('NotesEditor', 'ckeditor-extensions');
if (notesEditorExtensions?.length && this.useExtraPlugins) {
if (notesEditorExtensions?.length) {
notesEditorExtensions.forEach(notesEditorExtension => {
if (notesEditorExtension.extraPlugin) {
extraPlugins = `${extraPlugins},${notesEditorExtension.extraPlugin}`;
Expand All @@ -96,7 +97,7 @@ CKEDITOR.editorConfig = function (config) {
removePlugins = `${extraPlugins},${notesEditorExtension.removePlugin}`;
}
if (notesEditorExtension.extraToolbarItem) {
toolbar[0].push(notesEditorExtension.extraToolbarItem);
toolbar[toolbar.length - 1].items.push(notesEditorExtension.extraToolbarItem);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,16 @@ export default {
saveButtonIcon: 'fas fa-save',
translationSwitch: false,
newTranslation: false,
autosaveProcessedFromEditorExtension: false,
extensionDataUpdated: false
};
},
computed: {
saveOrUpdateDisabled() {
return (!this.note?.title || this.note?.title?.length < 3
|| this.note?.title?.length > this.titleMaxLength)
|| (this.noteNotModified
&& !this.propertiesModified && !this.draftNote) || this.savingDraft;
&& !this.propertiesModified && !this.draftNote && !this.note.draftPage) || this.savingDraft;
},
noteNotModified() {
return this.note?.title === this.originalNote?.title && this.$noteUtils.isSameContent(this.note?.content, this.originalNote?.content);
Expand Down Expand Up @@ -195,6 +197,7 @@ export default {
document.addEventListener('automatic-translation-extensions-updated', () => {
this.refreshTranslationExtensions();
});
document.addEventListener('note-editor-extensions-data-updated', (evt) => this.processAutoSaveFromEditorExtension(evt));
this.getAvailableLanguages();
window.addEventListener('beforeunload', () => {
if (!this.postingNote && this.note.draftPage && this.note.id) {
Expand Down Expand Up @@ -248,6 +251,65 @@ export default {
});
},
methods: {
processAutoSaveFromEditorExtension(event) {
if (event.detail.processAutoSave) {
this.extensionDataUpdated = true;
this.autosaveProcessedFromEditorExtension = true;
this.draftSavingStatus = this.$t('notes.draft.savingDraftStatus');
clearTimeout(this.saveDraft);
const draftNote = this.fillDraftNote();
if (!draftNote.title) {
draftNote.title = this.$t('notes.untitled.title');
}
draftNote.lang = this.selectedLanguage;
if (this.newDraft){
draftNote.id = null;
}
if (draftNote.properties) {
draftNote.properties.draft = true;
if (this.newTranslation && !this.featuredImageUpdated) {
draftNote.properties.featuredImage = null;
}
}
this.$notesService.saveDraftNote(draftNote, this.parentPageId).then(savedDraftNote => {
this.actualNote = {
id: savedDraftNote.id,
name: savedDraftNote.name,
title: savedDraftNote.title,
content: savedDraftNote.content,
author: savedDraftNote.author,
owner: savedDraftNote.owner,
properties: savedDraftNote.properties
};
this.newDraft=false;
savedDraftNote.parentPageId = this.parentPageId;
this.note = savedDraftNote;
localStorage.setItem(`draftNoteId-${this.note.id}-${this.selectedLanguage}`, JSON.stringify(savedDraftNote));
this.newTranslation = false;
}).then(() => {
this.savingDraft = false;
setTimeout(() => {
this.draftSavingStatus = this.$t('notes.draft.savedDraftStatus');
if (this.autosaveProcessedFromEditorExtension) {
document.dispatchEvent(new CustomEvent('note-draft-auto-save-done', {
detail: {
draftId: this.note.id
}
}));
}
this.autosaveProcessedFromEditorExtension = false;
}, this.autoSaveDelay);
}).catch(e => {
console.error('Error when creating draft note: ', e);
this.$root.$emit('show-alert', {
type: 'error',
message: this.$t(`notes.message.${e.message}`)
});
});
} else {
this.draftSavingStatus = this.$t('notes.draft.savedDraftStatus');
}
},
editorClosed() {
window.close();
},
Expand Down Expand Up @@ -299,7 +361,8 @@ export default {
parentPageId: this.note?.draftPage && this.note?.targetPageId === this.parentPageId ? null : this.parentPageId,
toBePublished: false,
appName: this.appName,
properties: properties
properties: properties,
extensionDataUpdated: this.extensionDataUpdated
};
if (note.id) {
this.updateNote(note);
Expand Down Expand Up @@ -332,9 +395,14 @@ export default {
}).finally(() => {
this.enableClickOnce();
this.removeLocalStorageCurrentDraft(currentDraftId);
this.extensionDataUpdated = false;
});
},
createNote(note) {
note.properties = {
draft: this.note?.draftPage,
noteId: this.note?.id
};
return this.$notesService.createNote(note).then(data => {
const draftNote = JSON.parse(localStorage.getItem(`draftNoteId-${this.note.id}-${this.selectedLanguage}`));
this.note = data;
Expand Down
Loading

0 comments on commit f454009

Please sign in to comment.