import TabAwareWidget from "./tab_aware_widget.js";
import libraryLoader from "../services/library_loader.js";
import noteAutocompleteService from "../services/note_autocomplete.js";
import treeCache from "../services/tree_cache.js";
import server from "../services/server.js";
import ws from "../services/ws.js";
import SpacedUpdate from "../services/spaced_update.js";
import attributesParser from "../services/attribute_parser.js";
import AttributeDetailWidget from "./attribute_detail.js";
const mentionSetup = {
feeds: [
{
marker: '@',
feed: queryText => {
return new Promise((res, rej) => {
noteAutocompleteService.autocompleteSource(queryText, rows => {
res(rows.map(row => {
return {
id: '@' + row.notePathTitle,
name: row.notePathTitle,
link: '#' + row.notePath,
notePath: row.notePath,
highlightedNotePathTitle: row.highlightedNotePathTitle
}
}));
});
});
},
itemRenderer: item => {
const itemElement = document.createElement('span');
itemElement.classList.add('mentions-item');
itemElement.innerHTML = `${item.highlightedNotePathTitle} `;
return itemElement;
},
minimumCharacters: 0
},
{
marker: '#',
feed: async queryText => {
const names = await server.get(`attributes/names/?type=label&query=${encodeURIComponent(queryText)}`);
return names.map(name => {
return {
id: '#' + name,
name: name
}
});
},
minimumCharacters: 0,
attributeMention: true
},
{
marker: '~',
feed: async queryText => {
const names = await server.get(`attributes/names/?type=relation&query=${encodeURIComponent(queryText)}`);
return names.map(name => {
return {
id: '~' + name,
name: name
}
});
},
minimumCharacters: 0,
attributeMention: true
}
]
};
const TPL = `
`;
export default class NoteAttributesWidget extends TabAwareWidget {
constructor() {
super();
this.attributeDetailWidget = new AttributeDetailWidget().setParent(this);
this.spacedUpdate = new SpacedUpdate(() => {
this.parseAttributes();
this.attributeDetailWidget.hide();
});
}
doRender() {
this.$widget = $(TPL);
this.$editor = this.$widget.find('.note-attributes-editor');
this.initialized = this.initEditor();
this.$attrDisplay = this.$widget.find('.attr-display');
this.$ownedExpander = this.$widget.find('.attr-owned-expander');
this.$ownedExpander.on('click', () => {
if (this.$attrDisplay.is(":visible")) {
this.$attrDisplay.slideUp(200);
}
else {
this.$attrDisplay.slideDown(200);
}
});
this.$ownedExpanderText = this.$ownedExpander.find('.attr-expander-text');
this.$inheritedAttributes = this.$widget.find('.inherited-attributes');
this.$inheritedExpander = this.$widget.find('.attr-inherited-expander');
this.$inheritedExpander.on('click', () => {
if (this.$inheritedAttributes.is(":visible")) {
this.$inheritedAttributes.slideUp(200);
}
else {
this.$inheritedAttributes.slideDown(200);
}
});
this.$inheritedExpanderText = this.$inheritedExpander.find('.attr-expander-text');
this.$inheritedEmptyExpander = this.$widget.find('.attr-inherited-empty-expander');
this.$editor.on('keydown', async e => {
const keycode = (e.keyCode ? e.keyCode : e.which);
if (keycode === 13) {
this.triggerCommand('focusOnDetail', {tabId: this.tabContext.tabId});
await this.save();
}
this.attributeDetailWidget.hide();
});
this.$editor.on('blur', () => {
this.save();
this.attributeDetailWidget.hide();
});
this.$widget.append(this.attributeDetailWidget.render());
}
async save() {
const attributes = this.parseAttributes();
if (attributes) {
await server.put(`notes/${this.noteId}/attributes2`, attributes, this.componentId);
}
}
parseAttributes() {
try {
const attrs = attributesParser.lexAndParse(this.textEditor.getData());
this.$widget.removeClass("error");
this.$widget.removeAttr("title");
this.$ownedExpander.removeClass("error");
this.$ownedExpanderText.text(attrs.length + ' owned ' + this.attrPlural(attrs.length));
return attrs;
}
catch (e) {
this.$widget.attr("title", e.message);
this.$widget.addClass("error");
this.$ownedExpander.addClass("error");
this.$ownedExpanderText.text(e.message);
}
}
async initEditor() {
await libraryLoader.requireLibrary(libraryLoader.CKEDITOR);
// CKEditor since version 12 needs the element to be visible before initialization. At the same time
// we want to avoid flicker - i.e. show editor only once everything is ready. That's why we have separate
// display of $widget in both branches.
this.$widget.show();
this.$editor.on("click", async e => {
const pos = this.textEditor.model.document.selection.getFirstPosition();
if (pos && pos.textNode && pos.textNode.data) {
const attrText = pos.textNode.data;
const clickIndex = pos.offset - pos.textNode.startOffset;
const parsedAttrs = attributesParser.lexAndParse(attrText, true);
let matchedAttr = null;
for (const attr of parsedAttrs) {
if (clickIndex >= attr.startIndex && clickIndex <= attr.endIndex) {
matchedAttr = attr;
break;
}
}
this.attributeDetailWidget.showAttributeDetail(matchedAttr, e.pageX, e.pageY);
}
});
this.textEditor = await BalloonEditor.create(this.$editor[0], {
removePlugins: [
'Enter',
'ShiftEnter',
'Heading',
'Link',
'Autoformat',
'Bold',
'Italic',
'Underline',
'Strikethrough',
'Code',
'Superscript',
'Subscript',
'BlockQuote',
'Image',
'ImageCaption',
'ImageStyle',
'ImageToolbar',
'ImageUpload',
'ImageResize',
'List',
'TodoList',
'PasteFromOffice',
'Table',
'TableToolbar',
'TableProperties',
'TableCellProperties',
'Indent',
'IndentBlock',
'BlockToolbar',
'ParagraphButtonUI',
'HeadingButtonsUI',
'UploadimagePlugin',
'InternalLinkPlugin',
'MarkdownImportPlugin',
'CuttonotePlugin',
'TextTransformation',
'Font',
'FontColor',
'FontBackgroundColor',
'CodeBlock',
'SelectAll',
'IncludeNote',
'CutToNote'
],
toolbar: {
items: []
},
placeholder: "Type the labels and relations here ...",
mention: mentionSetup
});
this.textEditor.model.document.on('change:data', () => this.spacedUpdate.scheduleUpdate());
// disable spellcheck for attribute editor
this.textEditor.editing.view.change( writer => {
writer.setAttribute( 'spellcheck', 'false', this.textEditor.editing.view.document.getRoot() );
} );
//await import(/* webpackIgnore: true */'../../libraries/ckeditor/inspector.js');
//CKEditorInspector.attach(this.textEditor);
}
async loadReferenceLinkTitle(noteId, $el) {
const note = await treeCache.getNote(noteId, true);
let title;
if (!note) {
title = '[missing]';
}
else if (!note.isDeleted) {
title = note.title;
}
else {
title = note.isErased ? '[erased]' : `${note.title} (deleted)`;
}
$el.text(title);
}
async refreshWithNote(note) {
const ownedAttributes = note.getOwnedAttributes();
const $attributesContainer = $("