2021-07-16 16:41:58 +02:00
|
|
|
/*
|
2021-12-25 23:39:34 +01:00
|
|
|
* Copyright [2021] [wisemapping]
|
2021-07-16 16:41:58 +02:00
|
|
|
*
|
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2021-12-05 18:25:16 +01:00
|
|
|
import { $defined } from '@wisemapping/core-js';
|
|
|
|
import $ from 'jquery';
|
2022-01-30 11:37:14 +01:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
import Events from './Events';
|
|
|
|
import ActionDispatcher from './ActionDispatcher';
|
2022-02-17 07:24:41 +01:00
|
|
|
import Topic from './Topic';
|
2021-12-05 18:25:16 +01:00
|
|
|
|
2021-12-05 00:39:20 +01:00
|
|
|
class MultilineTextEditor extends Events {
|
2022-02-17 07:24:41 +01:00
|
|
|
private _topic: Topic;
|
|
|
|
|
|
|
|
private _containerElem: JQuery;
|
|
|
|
|
2021-12-05 00:39:20 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
2021-10-05 02:05:34 +02:00
|
|
|
this._topic = null;
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private static _buildEditor() {
|
2021-10-05 02:05:34 +02:00
|
|
|
const result = $('<div></div>')
|
|
|
|
.attr('id', 'textContainer')
|
|
|
|
.css({
|
|
|
|
display: 'none',
|
|
|
|
zIndex: '8',
|
|
|
|
border: '0 none',
|
|
|
|
});
|
|
|
|
|
|
|
|
const textareaElem = $('<textarea tabindex="-1" value="" wrap="off" ></textarea>')
|
|
|
|
.css({
|
|
|
|
border: '1px gray dashed',
|
2022-02-23 06:25:09 +01:00
|
|
|
background: 'rgba(98, 135, 167, .4)',
|
2021-10-05 02:05:34 +02:00
|
|
|
outline: '0 none',
|
|
|
|
resize: 'none',
|
|
|
|
overflow: 'hidden',
|
2022-02-23 06:25:09 +01:00
|
|
|
padding: '2px 0px 2px 4px',
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
result.append(textareaElem);
|
|
|
|
return result;
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _registerEvents(containerElem: JQuery) {
|
2021-10-05 02:05:34 +02:00
|
|
|
const textareaElem = this._getTextareaElem();
|
2022-02-17 07:24:41 +01:00
|
|
|
textareaElem.on('keydown', (event) => {
|
2022-04-05 20:07:13 +02:00
|
|
|
switch (event.code) {
|
|
|
|
case 'Escape':
|
2022-02-17 07:24:41 +01:00
|
|
|
this.close(false);
|
2021-10-05 02:05:34 +02:00
|
|
|
break;
|
2022-04-05 20:07:13 +02:00
|
|
|
case 'Enter': {
|
2021-10-05 02:05:34 +02:00
|
|
|
if (event.metaKey || event.ctrlKey) {
|
|
|
|
// Add return ...
|
2022-02-17 07:24:41 +01:00
|
|
|
const text = this._getTextAreaText();
|
|
|
|
const cursorPosition = text.length;
|
2021-10-05 02:05:34 +02:00
|
|
|
const head = text.substring(0, cursorPosition);
|
|
|
|
let tail = '';
|
|
|
|
if (cursorPosition < text.length) {
|
|
|
|
tail = text.substring(cursorPosition, text.length);
|
|
|
|
}
|
|
|
|
textareaElem.val(`${head}\n${tail}`);
|
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
textareaElem.focus();
|
|
|
|
textareaElem[0].setSelectionRange(cursorPosition + 1, cursorPosition + 1);
|
2021-10-05 02:05:34 +02:00
|
|
|
} else {
|
2022-02-17 07:24:41 +01:00
|
|
|
this.close(true);
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
|
|
|
break;
|
2021-12-19 18:07:01 +01:00
|
|
|
}
|
2021-12-15 03:04:47 +01:00
|
|
|
default:
|
|
|
|
// No actions...
|
|
|
|
break;
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
textareaElem.on('keypress', (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
textareaElem.on('keyup', (event) => {
|
2022-02-17 07:24:41 +01:00
|
|
|
const text = this._getTextareaElem().val();
|
|
|
|
this.fireEvent('input', [event, text]);
|
|
|
|
this._adjustEditorSize();
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// If the user clicks on the input, all event must be ignored ...
|
|
|
|
containerElem.on('click', (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
containerElem.on('dblclick', (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
containerElem.on('mousedown', (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _adjustEditorSize() {
|
2021-10-05 02:05:34 +02:00
|
|
|
if (this.isVisible()) {
|
|
|
|
const textElem = this._getTextareaElem();
|
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
const lines = this._getTextAreaText().split('\n');
|
2021-10-05 02:05:34 +02:00
|
|
|
let maxLineLength = 1;
|
2022-02-23 06:25:09 +01:00
|
|
|
lines.forEach((line: string) => {
|
|
|
|
maxLineLength = Math.max(line.length, maxLineLength);
|
2021-10-05 02:05:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
textElem.attr('cols', maxLineLength);
|
|
|
|
textElem.attr('rows', lines.length);
|
|
|
|
|
|
|
|
this._containerElem.css({
|
2022-02-23 06:25:09 +01:00
|
|
|
width: `${maxLineLength + 2}em`,
|
2021-10-05 02:05:34 +02:00
|
|
|
height: textElem.height(),
|
|
|
|
});
|
|
|
|
}
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
isVisible(): boolean {
|
2021-12-05 18:25:16 +01:00
|
|
|
return $defined(this._containerElem) && this._containerElem.css('display') === 'block';
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _updateModel() {
|
|
|
|
if (this._topic.getText() !== this._getTextAreaText()) {
|
|
|
|
const text = this._getTextAreaText();
|
2021-10-05 02:05:34 +02:00
|
|
|
const topicId = this._topic.getId();
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-10-05 02:05:34 +02:00
|
|
|
const actionDispatcher = ActionDispatcher.getInstance();
|
2022-03-06 16:27:28 +01:00
|
|
|
try {
|
|
|
|
actionDispatcher.changeTextToTopic([topicId], text);
|
|
|
|
} catch (e) {
|
|
|
|
// Hack: For some reasom, editor seems to end up connexted to a deleted node.
|
|
|
|
// More research required.
|
|
|
|
console.error(`Text could not be update -> ${JSON.stringify(e)}`);
|
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
show(topic: Topic, text: string): void {
|
2021-10-05 02:05:34 +02:00
|
|
|
// Close a previous node editor if it's opened ...
|
|
|
|
if (this._topic) {
|
|
|
|
this.close(false);
|
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-10-05 02:05:34 +02:00
|
|
|
this._topic = topic;
|
|
|
|
if (!this.isVisible()) {
|
|
|
|
// Create editor ui
|
2021-12-05 18:25:16 +01:00
|
|
|
const containerElem = MultilineTextEditor._buildEditor();
|
2021-10-05 02:05:34 +02:00
|
|
|
$('body').append(containerElem);
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-10-05 02:05:34 +02:00
|
|
|
this._containerElem = containerElem;
|
|
|
|
this._registerEvents(containerElem);
|
|
|
|
this._showEditor(text);
|
|
|
|
}
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _showEditor(defaultText: string) {
|
2021-10-05 02:05:34 +02:00
|
|
|
const topic = this._topic;
|
|
|
|
|
|
|
|
// Hide topic text ...
|
|
|
|
topic.getTextShape().setVisibility(false);
|
|
|
|
|
|
|
|
// Set Editor Style
|
|
|
|
const nodeText = topic.getTextShape();
|
2022-01-03 22:01:16 +01:00
|
|
|
const fontStyle = nodeText.getFontStyle();
|
|
|
|
fontStyle.size = nodeText.getHtmlFontSize();
|
|
|
|
fontStyle.color = nodeText.getColor();
|
|
|
|
this._setStyle(fontStyle);
|
2021-12-15 03:04:47 +01:00
|
|
|
|
2021-10-05 02:05:34 +02:00
|
|
|
// Set editor's initial size
|
2022-02-17 07:24:41 +01:00
|
|
|
// Position the editor and set the size...
|
|
|
|
const textShape = topic.getTextShape();
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
this._containerElem.css('display', 'block');
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-23 06:25:09 +01:00
|
|
|
let { top, left } = textShape.getNativePosition();
|
|
|
|
// Adjust padding top position ...
|
|
|
|
top -= 4;
|
|
|
|
left -= 4;
|
|
|
|
this._containerElem.offset({ top, left });
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
// Set editor's initial text ...
|
|
|
|
const text = $defined(defaultText) ? defaultText : topic.getText();
|
|
|
|
this._setText(text);
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
// Set the element focus and select the current text ...
|
|
|
|
const inputElem = this._getTextareaElem();
|
|
|
|
this._positionCursor(inputElem, !$defined(defaultText));
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _setStyle(fontStyle) {
|
2021-10-05 02:05:34 +02:00
|
|
|
const inputField = this._getTextareaElem();
|
2021-12-20 21:54:31 +01:00
|
|
|
// allowed param reassign to avoid risks of existing code relying in this side-effect
|
|
|
|
/* eslint-disable no-param-reassign */
|
2022-01-03 22:01:16 +01:00
|
|
|
if (!$defined(fontStyle.fontFamily)) {
|
|
|
|
fontStyle.fontFamily = 'Arial';
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
|
|
|
if (!$defined(fontStyle.style)) {
|
|
|
|
fontStyle.style = 'normal';
|
|
|
|
}
|
|
|
|
if (!$defined(fontStyle.weight)) {
|
|
|
|
fontStyle.weight = 'normal';
|
|
|
|
}
|
|
|
|
if (!$defined(fontStyle.size)) {
|
|
|
|
fontStyle.size = 12;
|
|
|
|
}
|
2021-12-20 21:54:31 +01:00
|
|
|
/* eslint-enable no-param-reassign */
|
2021-10-05 02:05:34 +02:00
|
|
|
const style = {
|
|
|
|
fontSize: `${fontStyle.size}px`,
|
2022-01-03 22:01:16 +01:00
|
|
|
fontFamily: fontStyle.fontFamily,
|
2021-10-05 02:05:34 +02:00
|
|
|
fontStyle: fontStyle.style,
|
|
|
|
fontWeight: fontStyle.weight,
|
|
|
|
color: fontStyle.color,
|
|
|
|
};
|
|
|
|
inputField.css(style);
|
|
|
|
this._containerElem.css(style);
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 20:25:36 +01:00
|
|
|
private _setText(text: string): void {
|
2021-10-05 02:05:34 +02:00
|
|
|
const textareaElem = this._getTextareaElem();
|
|
|
|
textareaElem.val(text);
|
|
|
|
this._adjustEditorSize();
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _getTextAreaText(): string {
|
|
|
|
return this._getTextareaElem().val() as string;
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _getTextareaElem(): JQuery<HTMLTextAreaElement> {
|
2021-10-05 02:05:34 +02:00
|
|
|
return this._containerElem.find('textarea');
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
private _positionCursor(textareaElem: JQuery<HTMLTextAreaElement>, selectText: boolean) {
|
2021-10-05 02:05:34 +02:00
|
|
|
textareaElem.focus();
|
2022-02-17 07:24:41 +01:00
|
|
|
const lengh = this._getTextAreaText().length;
|
2021-10-05 02:05:34 +02:00
|
|
|
if (selectText) {
|
|
|
|
// Mark text as selected ...
|
2022-02-17 07:24:41 +01:00
|
|
|
textareaElem[0].setSelectionRange(0, lengh);
|
2021-10-05 02:05:34 +02:00
|
|
|
} else {
|
2021-12-15 03:28:15 +01:00
|
|
|
textareaElem.focus();
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2022-02-17 07:24:41 +01:00
|
|
|
close(update: boolean): void {
|
2022-02-18 17:23:57 +01:00
|
|
|
if (this.isVisible()) {
|
|
|
|
if (update) {
|
|
|
|
this._updateModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove it form the screen ...
|
|
|
|
this._containerElem.remove();
|
|
|
|
this._containerElem = null;
|
2021-07-16 16:41:58 +02:00
|
|
|
}
|
2022-02-18 17:11:33 +01:00
|
|
|
|
2022-02-18 17:23:57 +01:00
|
|
|
if (this._topic) {
|
|
|
|
this._topic.getTextShape().setVisibility(true);
|
|
|
|
this._topic = null;
|
|
|
|
}
|
2021-12-05 00:39:20 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
export default MultilineTextEditor;
|