NEW ACE Editor is restored at same cursor position after a save

This commit is contained in:
Laurent Destailleur 2022-02-27 14:46:40 +01:00
parent acf6d19500
commit b598671484
2 changed files with 35 additions and 16 deletions

View File

@ -44,6 +44,8 @@ class DolEditor
public $height;
public $width;
public $readonly;
public $posx;
public $posy;
/**
@ -64,8 +66,9 @@ class DolEditor
* @param int $rows Size of rows for textarea tool
* @param string $cols Size of cols for textarea tool (textarea number of cols '70' or percent 'x%')
* @param int $readonly 0=Read/Edit, 1=Read only
* @param array $poscursor Array for initial cursor position array('x'=>x, 'y'=>y)
*/
public function __construct($htmlname, $content, $width = '', $height = 200, $toolbarname = 'Basic', $toolbarlocation = 'In', $toolbarstartexpanded = false, $uselocalbrowser = true, $okforextendededitor = true, $rows = 0, $cols = 0, $readonly = 0)
public function __construct($htmlname, $content, $width = '', $height = 200, $toolbarname = 'Basic', $toolbarlocation = 'In', $toolbarstartexpanded = false, $uselocalbrowser = true, $okforextendededitor = true, $rows = 0, $cols = 0, $readonly = 0, $poscursor = array())
{
global $conf, $langs;
@ -106,8 +109,10 @@ class DolEditor
$this->toolbarstartexpanded = $toolbarstartexpanded;
$this->rows = max(ROWS_3, $rows);
$this->cols = (preg_match('/%/', $cols) ? $cols : max(40, $cols)); // If $cols is a percent, we keep it, otherwise, we take max
$this->height = $height;
$this->height = $height;
$this->width = $width;
$this->posx = empty($poscursor['x']) ? 0 : $poscursor['x'];
$this->posy = empty($poscursor['y']) ? 0 : $poscursor['y'];
}
}
@ -257,9 +262,12 @@ class DolEditor
$out .= '<script type="text/javascript">'."\n";
$out .= 'jQuery(document).ready(function() {'."\n";
$out .= ' var aceEditor = window.ace.edit("'.$this->htmlname.'aceeditorid");
aceEditor.moveCursorTo('.($this->posy+1).','.$this->posx.');
aceEditor.gotoLine('.($this->posy+1).','.$this->posx.');
var StatusBar = window.ace.require("ace/ext/statusbar").StatusBar; // Init status bar. Need lib ext-statusbar
var statusBar = new StatusBar(aceEditor, document.getElementById("statusBar'.$this->htmlname.'")); // Init status bar. Need lib ext-statusbar
var oldNbOfLines = 0
var oldNbOfLines = 0;
jQuery(".morelines'.$this->htmlname.'").click(function() {
var aceEditorClicked = window.ace.edit("'.$this->htmlname.'aceeditorid");
currentline = aceEditorClicked.getOption("maxLines");
@ -322,8 +330,8 @@ class DolEditor
var cursorPos = aceEditor.getCursorPosition();
console.log(cursorPos);
if (cursorPos) {
jQuery("#'.$this->htmlname.'_x").val(cursorPos.row);
jQuery("#'.$this->htmlname.'_y").val(cursorPos.column);
jQuery("#'.$this->htmlname.'_x").val(cursorPos.column);
jQuery("#'.$this->htmlname.'_y").val(cursorPos.row);
}
//console.log(aceEditor.getSession().getValue());
// Inject content of editor into the original HTML field.

View File

@ -3414,7 +3414,8 @@ if ($action == 'editcss') {
print $form->textwithpicto($langs->trans('WEBSITE_CSS_INLINE'), $htmlhelp, 1, 'help', '', 0, 2, 'csstooltip');
print '</td><td>';
$doleditor = new DolEditor('WEBSITE_CSS_INLINE', $csscontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '');
$poscursor = array('x'=>GETPOST('WEBSITE_CSS_INLINE_x'), 'y'=>GETPOST('WEBSITE_CSS_INLINE_y'));
$doleditor = new DolEditor('WEBSITE_CSS_INLINE', $csscontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, 'CSS', 'css');
print '</td></tr>';
@ -3427,7 +3428,8 @@ if ($action == 'editcss') {
print '</td><td>';
$doleditor = new DolEditor('WEBSITE_JS_INLINE', $jscontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '');
$poscursor = array('x'=>GETPOST('WEBSITE_JS_INLINE_x'), 'y'=>GETPOST('WEBSITE_JS_INLINE_y'));
$doleditor = new DolEditor('WEBSITE_JS_INLINE', $jscontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, 'JS', 'javascript');
print '</td></tr>';
@ -3442,7 +3444,8 @@ if ($action == 'editcss') {
print $form->textwithpicto($textwithhelp, $htmlhelp2, 1, 'warning', '', 0, 2, 'htmlheadertooltip2');
print '</td><td>';
$doleditor = new DolEditor('WEBSITE_HTML_HEADER', $htmlheadercontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '');
$poscursor = array('x'=>GETPOST('WEBSITE_HTML_HEADER_x'), 'y'=>GETPOST('WEBSITE_HTML_HEADER_y'));
$doleditor = new DolEditor('WEBSITE_HTML_HEADER', $htmlheadercontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, 'HTML Header', 'html');
print '</td></tr>';
@ -3452,7 +3455,8 @@ if ($action == 'editcss') {
print $langs->trans('WEBSITE_ROBOT');
print '</td><td>';
$doleditor = new DolEditor('WEBSITE_ROBOT', $robotcontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '');
$poscursor = array('x'=>GETPOST('WEBSITE_ROBOT_x'), 'y'=>GETPOST('WEBSITE_ROBOT_y'));
$doleditor = new DolEditor('WEBSITE_ROBOT', $robotcontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, 'Robot file', 'text');
print '</td></tr>';
@ -3462,7 +3466,8 @@ if ($action == 'editcss') {
print $langs->trans('WEBSITE_HTACCESS');
print '</td><td>';
$doleditor = new DolEditor('WEBSITE_HTACCESS', $htaccesscontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '');
$poscursor = array('x'=>GETPOST('WEBSITE_HTACCESS_x'), 'y'=>GETPOST('WEBSITE_HTACCESS_y'));
$doleditor = new DolEditor('WEBSITE_HTACCESS', $htaccesscontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, $langs->trans("File").' .htaccess', 'text');
print '</td></tr>';
@ -3474,7 +3479,9 @@ if ($action == 'editcss') {
print $form->textwithpicto($langs->trans('WEBSITE_MANIFEST_JSON'), $htmlhelp, 1, 'help', '', 0, 2, 'manifestjsontooltip');
print '</td><td>';
print $langs->trans("UseManifest").': '.$form->selectyesno('use_manifest', $website->use_manifest, 1).'<br>';
$doleditor = new DolEditor('WEBSITE_MANIFEST_JSON', $manifestjsoncontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '');
$poscursor = array('x'=>GETPOST('WEBSITE_MANIFEST_JSON_x'), 'y'=>GETPOST('WEBSITE_MANIFEST_JSON_y'));
$doleditor = new DolEditor('WEBSITE_MANIFEST_JSON', $manifestjsoncontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, $langs->trans("File").' manifest.json', 'text');
print '</td></tr>';
@ -3484,7 +3491,8 @@ if ($action == 'editcss') {
print $form->textwithpicto($langs->trans('WEBSITE_README'), $htmlhelp, 1, 'help', '', 0, 2, 'readmetooltip');
print '</td><td>';
$doleditor = new DolEditor('WEBSITE_README', $readmecontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '');
$poscursor = array('x'=>GETPOST('WEBSITE_README_x'), 'y'=>GETPOST('WEBSITE_README_y'));
$doleditor = new DolEditor('WEBSITE_README', $readmecontent, '', '220', 'ace', 'In', true, false, 'ace', 0, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, $langs->trans("File").' README.md', 'text');
print '</td></tr>';
@ -3998,7 +4006,8 @@ if ($action == 'editmeta' || $action == 'createcontainer') { // Edit properties
$htmlhelp .= dol_htmlentitiesbr($htmlheadercontentdefault);
print $form->textwithpicto($langs->trans('HtmlHeaderPage'), $htmlhelp, 1, 'help', '', 0, 2, 'htmlheadertooltip');
print '</td><td>';
$doleditor = new DolEditor('htmlheader', $pagehtmlheader, '', '120', 'ace', 'In', true, false, 'ace', ROWS_3, '100%', '');
$poscursor = array('x'=>GETPOST('htmlheader_x'), 'y'=>GETPOST('htmlheader_y'));
$doleditor = new DolEditor('htmlheader', $pagehtmlheader, '', '120', 'ace', 'In', true, false, 'ace', ROWS_3, '100%', '', $poscursor);
print $doleditor->Create(1, '', true, 'HTML Header', 'html');
print '</td></tr>';
@ -4142,9 +4151,10 @@ if ($action == 'editsource') {
$maxheightwin = $_SESSION["dol_screenheight"] - 490;
}
}
//var_dump($_SESSION["dol_screenheight"]);
$poscursor = array('x'=>GETPOST('PAGE_CONTENT_x'), 'y'=>GETPOST('PAGE_CONTENT_y'));
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
$doleditor = new DolEditor('PAGE_CONTENT', $contentforedit, '', $maxheightwin, 'Full', '', true, true, 'ace', ROWS_5, '40%');
$doleditor = new DolEditor('PAGE_CONTENT', $contentforedit, '', $maxheightwin, 'Full', '', true, true, 'ace', ROWS_5, '40%', 0, $poscursor);
$doleditor->Create(0, '', false, 'HTML Source', 'php');
}
@ -4161,7 +4171,8 @@ if ($action == 'editsource') {
$contentforedit = preg_replace('/(<img.*src=")(?!http)/', '\1'.DOL_URL_ROOT.'/viewimage.php?modulepart=medias&file=', $contentforedit, -1, $nbrep);
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
$doleditor=new DolEditor('PAGE_CONTENT',$contentforedit,'',500,'Full','',true,true,true,ROWS_5,'90%');
$poscursor = array('x'=>GETPOST('PAGE_CONTENT_x'), 'y'=>GETPOST('PAGE_CONTENT_y'));
$doleditor=new DolEditor('PAGE_CONTENT',$contentforedit,'',500,'Full','',true,true,true,ROWS_5,'90%',$poscursor);
$doleditor->Create(0, '', false);
}*/