Fix cursor issues in forms realtime edition

pull/1/head
yflory 4 years ago
parent fe9d17cd17
commit 9f5cf8daf6

@ -19,7 +19,9 @@ define([
'/customize/application_config.js', '/customize/application_config.js',
'/common/diffMarked.js', '/common/diffMarked.js',
'/common/sframe-common-codemirror.js', '/common/sframe-common-codemirror.js',
'/common/text-cursor.js',
'cm/lib/codemirror', 'cm/lib/codemirror',
'/bower_components/chainpad/chainpad.dist.js',
'/common/inner/share.js', '/common/inner/share.js',
'/common/inner/access.js', '/common/inner/access.js',
@ -61,7 +63,9 @@ define([
AppConfig, AppConfig,
DiffMd, DiffMd,
SFCodeMirror, SFCodeMirror,
TextCursor,
CMeditor, CMeditor,
ChainPad,
Share, Access, Properties, Share, Access, Properties,
Flatpickr, Flatpickr,
Sortable Sortable
@ -195,12 +199,6 @@ define([
if (tmp && tmp.cursor) { if (tmp && tmp.cursor) {
cursor = tmp.cursor; cursor = tmp.cursor;
} }
/*
if (tmp && tmp.content && Sortify(v) === Sortify(tmp.old)) {
v = tmp.content;
cursor = tmp.cursor;
}
*/
// Checkbox: max options // Checkbox: max options
var maxOptions, maxInput; var maxOptions, maxInput;
@ -287,6 +285,12 @@ define([
// if this element was active before the remote change, restore cursor // if this element was active before the remote change, restore cursor
var setCursor = function () { var setCursor = function () {
if (v.type && v.type !== 'text') { return; } if (v.type && v.type !== 'text') { return; }
try {
var ops = ChainPad.Diff.diff(cursor.el, val);
['start', 'end'].forEach(function (attr) {
cursor[attr] = TextCursor.transformCursor(cursor[attr], ops);
});
} catch (e) { console.error(e); }
input.selectionStart = cursor.start || 0; input.selectionStart = cursor.start || 0;
input.selectionEnd = cursor.end || 0; input.selectionEnd = cursor.end || 0;
setTimeout(function () { input.focus(); }); setTimeout(function () { input.focus(); });
@ -493,7 +497,6 @@ define([
// Set cursor getter (to handle remote changes to the form) // Set cursor getter (to handle remote changes to the form)
setCursorGetter(function () { setCursorGetter(function () {
var values = [];
var active = document.activeElement; var active = document.activeElement;
var cursor = {}; var cursor = {};
$container.find('input').each(function (i, el) { $container.find('input').each(function (i, el) {
@ -503,44 +506,21 @@ define([
cursor.start = el.selectionStart; cursor.start = el.selectionStart;
cursor.end = el.selectionEnd; cursor.end = el.selectionEnd;
} }
values.push(val);
}); });
if (v.type === "day") {
var dayPickr = $(calendarView).find('input')[0]._flatpickr;
values = dayPickr.selectedDates.map(function (date) {
return +date;
});
}
var _content = {values: values};
if (maxInput) {
_content.max = Number($(maxInput).val()) || 1;
}
if (typeSelect) {
_content.type = typeSelect.getValue();
}
if (v.items) { if (v.items) {
var items = []; var items = [];
$(containerItems).find('input').each(function (i, el) { $(containerItems).find('input').each(function (i, el) {
var val = $(el).val() || el.placeholder || '';
if (el === active) { if (el === active) {
cursor.item = true; cursor.item = true;
cursor.uid= $(el).data('uid'); cursor.uid= $(el).data('uid');
cursor.start = el.selectionStart; cursor.start = el.selectionStart;
cursor.end = el.selectionEnd; cursor.end = el.selectionEnd;
cursor.el = val;
} }
var val = $(el).val() || el.placeholder || '';
items.push({
uid: $(el).data('uid'),
v: val
});
}); });
_content.items = items;
} }
return { return {
old: (tmp && tmp.old) || v,
content: _content,
cursor: cursor cursor: cursor
}; };
}); });
@ -901,35 +881,45 @@ define([
return { return {
tag: tag, tag: tag,
edit: function (cb, tmp) { edit: function (cb, tmp) {
// Cancel changes
var cancelBlock = saveAndCancelOptions(cb);
var text = opts.text;
var cursor;
if (tmp && tmp.cursor) {
cursor = tmp.cursor;
}
var block, editor;
if (tmp && tmp.block) {
block = tmp.block;
editor = tmp.editor;
}
if (!block || !editor) {
var t = h('textarea'); var t = h('textarea');
var block = h('div.cp-form-edit-options-block', [t]); block = h('div.cp-form-edit-options-block', [t]);
var cm = SFCodeMirror.create("gfm", CMeditor, t); var cm = SFCodeMirror.create("gfm", CMeditor, t);
var editor = cm.editor; editor = cm.editor;
editor.setOption('lineNumbers', true); editor.setOption('lineNumbers', true);
editor.setOption('lineWrapping', true); editor.setOption('lineWrapping', true);
editor.setOption('styleActiveLine', true); editor.setOption('styleActiveLine', true);
editor.setOption('readOnly', false); editor.setOption('readOnly', false);
var text = opts.text;
var cursor;
if (tmp && tmp.cursor) {
cursor = tmp.cursor;
} }
setTimeout(function () { setTimeout(function () {
editor.focus();
if (!(tmp && tmp.editor)) {
editor.setValue(text); editor.setValue(text);
if (cursor) {
if (Sortify(cursor.start) === Sortify(cursor.end)) {
editor.setCursor(cursor.start);
} else { } else {
editor.setSelection(cursor.start, cursor.end); SFCodeMirror.setValueAndCursor(editor, editor.getValue(), text);
}
} }
editor.refresh(); editor.refresh();
editor.save(); editor.save();
editor.focus(); editor.focus();
}); });
if (APP.common) {
if (APP.common && !(tmp && tmp.block)) {
var markdownTb = APP.common.createMarkdownToolbar(editor, { var markdownTb = APP.common.createMarkdownToolbar(editor, {
embed: function (mt) { embed: function (mt) {
editor.focus(); editor.focus();
@ -940,8 +930,6 @@ define([
$(markdownTb.toolbar).show(); $(markdownTb.toolbar).show();
cm.configureTheme(APP.common, function () {}); cm.configureTheme(APP.common, function () {});
} }
// Cancel changes
var cancelBlock = saveAndCancelOptions(cb);
var getContent = function () { var getContent = function () {
return { return {
@ -949,9 +937,13 @@ define([
}; };
}; };
editor.on('change', function () { if (tmp && tmp.onChange) {
editor.off('change', tmp.onChange);
}
var on = function () {
cb(getContent()); cb(getContent());
}); };
editor.on('change', on);
cursorGetter = function () { cursorGetter = function () {
if (document.activeElement && block.contains(document.activeElement)) { if (document.activeElement && block.contains(document.activeElement)) {
@ -961,9 +953,10 @@ define([
}; };
} }
return { return {
old: opts, cursor: cursor,
content: getContent(), block: block,
cursor: cursor editor: editor,
onChange: on
}; };
}; };

Loading…
Cancel
Save