J4 Issue ?
avatar astridx
astridx
6 Jun 2018

Steps to reproduce the issue

  1. Set default editor to codemirror in global configuration.

  2. Open com_content and start creating an article. See, that there are no errors in web console.

  3. Open the file
    administrator/components/com_content/forms/article.xml
    and add the text

<field
name="articletext2"
type="editor"
label="COM_CONTENT_FIELD_ARTICLETEXT_LABEL"
filter="JComponentHelper::filterText"
buttons="true"
/>

after the field with the name articletext

  1. Open file

/administrator/components/com_content/tmpl/article/edit.php

and add the text

<?php echo $this->form->getInput('articletext2'); ?>

after the text

<?php echo $this->form->getInput('articletext'); ?>

  1. Again open com_content and start creating an article. See an error in web console:
    /media/vendor/codemirror/mode/text/html/text/html.js:1 Failed to load resource: the server responded with a status of 404 (Not Found)

  2. Write the text "first editor" into the first editor and the text "second editor" into the second editor.

  3. Make sure, that the Cursur is in the first editor and click F10 to open full screen editing. You now expect the first editor to open in full screen mode. But it opens the second editor.

avatar astridx astridx - open - 6 Jun 2018
avatar joomla-cms-bot joomla-cms-bot - change - 6 Jun 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 6 Jun 2018
avatar astridx astridx - change - 6 Jun 2018
The description was changed
avatar astridx astridx - edited - 6 Jun 2018
avatar dgrammatiko
dgrammatiko - comment - 6 Jun 2018

@astridx here's the code to fix those bugs:

customElements.define('joomla-editor-codemirror', class extends HTMLElement {
	constructor() {
		super();

		this.instance = '';
		this.cm = '';
		this.host = window.location.origin;
		this.element = this.querySelector('textarea');
		this.refresh = this.refresh.bind(this);
		this.toggleFullScreen = this.toggleFullScreen.bind(this);
		this.closeFullScreen = this.closeFullScreen.bind(this);

		// Append the editor script
		if (!document.head.querySelector('#cm-editor')) {
			const cmPath = this.getAttribute('editor');
			const script1 = document.createElement('script');

			script1.src = `${this.host}/${cmPath}`;
			script1.id = 'cm-editor';
			script1.setAttribute('async', false);
			document.head.insertBefore(script1, this.file);
		}
	}

	static get observedAttributes() {
		return ['options'];
	}

	get options() { return JSON.parse(this.getAttribute('options')); }
	set options(value) { this.setAttribute('options', value); }

	attributeChangedCallback(attr, oldValue, newValue) {
		switch (attr) {
			case 'options':
			if (oldValue && newValue !== oldValue) {
				this.refresh(this.element);
			}

			break;
		}
	}

	connectedCallback() {
		const that = this;
		const buttons = [].slice.call(this.querySelectorAll('.editor-xtd-buttons .xtd-button'));
		this.checkElement('CodeMirror')
			.then(() => {
				// Append the addons script
				if (!document.head.querySelector('#cm-addons')) {
					const addonsPath = this.getAttribute('addons');
					const script2 = document.createElement('script');

					script2.src = `${this.host}/${addonsPath}`;
					script2.id = 'cm-addons';
					script2.setAttribute('async', false);
					document.head.insertBefore(script2, this.file)
				}

				this.checkElement('CodeMirror', 'findModeByName')
					.then(() => {
						// For mode autoloading.
						window.CodeMirror.modeURL = this.getAttribute('mod-path');

						// Fire this function any time an editor is created.
						window.CodeMirror.defineInitHook((editor) => {
							// Try to set up the mode
							const mode = window.CodeMirror.findModeByName(that.options.mode || '');

							if (mode) {
								window.CodeMirror.autoLoadMode(editor, mode.mode);
								editor.setOption('mode', mode.mime);
							} else {
								window.CodeMirror.autoLoadMode(editor, that.options.mode);
							}

							const map = {
								"Ctrl-Q": that.toggleFullScreen,
								[that.getAttribute('fs-combo')]: that.toggleFullScreen,
								'Esc': that.closeFullScreen,
							};

							editor.addKeyMap(map);

							// Handle gutter clicks (place or remove a marker).
							editor.on("gutterClick", function (ed, n, gutter) {
								if (gutter !== "CodeMirror-markergutter") {
									return;
								}

								const info = ed.lineInfo(n);
								const hasMarker = !!info.gutterMarkers && !!info.gutterMarkers["CodeMirror-markergutter"];
								ed.setGutterMarker(n, "CodeMirror-markergutter", hasMarker ? null : this.makeMarker());
							});

							// Some browsers do something weird with the fieldset which doesn't work well with CodeMirror. Fix it.
							if (this.parentNode.tagName.toLowerCase() === 'fieldset') {
								this.parentNode.style.minWidth = 0;
							}
						});

						/** Register Editor */
						this.instance = window.CodeMirror.fromTextArea(this.element, this.options);
						Joomla.editors.instances[this.element.id] = this.instance;
					});
			});
	}

	disconnectedCallback() {
		// Remove from the Joomla API
		delete Joomla.editors.instances[this.element.id];
	}

	refresh(element) {
		this.instance = window.CodeMirror.fromTextArea(element, this.options);
	}

	rafAsync() {
		return new Promise(resolve => {
			requestAnimationFrame(resolve);
		});
	}

	async checkElement(string1, string2) {
		if (string2) {
			while (typeof window[string1][string2] !== 'function') {
				await this.rafAsync()
			}
		} else {
			while (typeof window[string1] !== 'function') {
				await this.rafAsync()
			}
		}

		return true;
	}

	toggleFullScreen() {
		this.instance.setOption("fullScreen", !this.instance.getOption("fullScreen"));
	}

	closeFullScreen() {
		this.instance.getOption("fullScreen") && this.instance.setOption("fullScreen", false);
	}

	makeMarker() {
		const marker = document.createElement("div");
		marker.className = "CodeMirror-markergutter-mark";
		return marker;
	}
});

Please either you or the GSOC student do the PR for this...

@okonomiyaki3000 I'm not sure if codemirror's function keys are ok in the J3 branch. If you haven't already done this maybe you might want to back port this part:

							const map = {
								"Ctrl-Q": that.toggleFullScreen,
								[that.getAttribute('fs-combo')]: that.toggleFullScreen,
								'Esc': that.closeFullScreen,
							};

							editor.addKeyMap(map);
avatar okonomiyaki3000
okonomiyaki3000 - comment - 6 Jun 2018

Cool, thanks. I'll check it out.

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 7 Jun 2018

@astridx can you please check #18519 if its same Issue?

avatar franz-wohlkoenig franz-wohlkoenig - change - 7 Jun 2018
Status New Information Required
avatar franz-wohlkoenig franz-wohlkoenig - change - 7 Jun 2018
Category Plugins
avatar Anu1601CS
Anu1601CS - comment - 7 Jun 2018

@dgrammatiko Thanks :) I take a look on this.

avatar Anu1601CS
Anu1601CS - comment - 7 Jun 2018

@dgrammatiko I tested this. Now, error from the console is gone and all bugs are fixed. :) Thank you very much.
But, I have one question is this possible to make a fullscreen of an editor which is readonly: true ?

avatar astridx
astridx - comment - 7 Jun 2018

@franz-wohlkoenig
Yes, #18519 describes one point (I did not mention the outputs erratic string) of this issue.

avatar dgrammatiko
dgrammatiko - comment - 7 Jun 2018

About the xtd buttons: I’ve observed also the problem and the solution should be similar to what we’ve done in tinymce (eg the modal ids need to be unique for each instance of the editor), probably this needs to be patched in the staging repo and the 4 branch as well.

About the readonly: I don’t see any problem with that although I haven’t tested this case

avatar franz-wohlkoenig
franz-wohlkoenig - comment - 7 Jun 2018

@astridx thanks for investigation, #18519 is closed in favor of this Issue.

avatar joomla-cms-bot joomla-cms-bot - change - 7 Jun 2018
Closed_By franz-wohlkoenig joomla-cms-bot
avatar joomla-cms-bot joomla-cms-bot - close - 7 Jun 2018
avatar joomla-cms-bot
joomla-cms-bot - comment - 7 Jun 2018
avatar franz-wohlkoenig franz-wohlkoenig - change - 7 Jun 2018
Status Information Required Closed
Closed_Date 0000-00-00 00:00:00 2018-06-07 12:43:01
Closed_By franz-wohlkoenig
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 7 Jun 2018

closed as having Pull Request #20684

avatar astridx
astridx - comment - 7 Jun 2018

@franz-wohlkoenig I made a mistake this morning. The issue #18519 is not solved with the PR to this issue. Can you please reopen this issue again?

avatar brianteeman brianteeman - change - 7 Jun 2018
Status Closed New
Closed_Date 2018-06-07 12:43:01
Closed_By joomla-cms-bot
avatar brianteeman brianteeman - reopen - 7 Jun 2018
avatar brianteeman
brianteeman - comment - 7 Jun 2018

re-opened as requested

avatar franz-wohlkoenig franz-wohlkoenig - change - 8 Jun 2018
Status New Discussion
avatar brianteeman brianteeman - change - 8 Jun 2018
Labels Added: J4 Issue
avatar brianteeman brianteeman - labeled - 8 Jun 2018
avatar okonomiyaki3000
okonomiyaki3000 - comment - 11 Jun 2018

By the way, this does not appear to be a problem in J3 but please let me know if I'm wrong about that.

avatar astridx
astridx - comment - 24 Jun 2018

Close as #20684 solve the issue.

avatar astridx astridx - change - 24 Jun 2018
Status Discussion Closed
Closed_Date 0000-00-00 00:00:00 2018-06-24 20:44:36
Closed_By astridx
avatar astridx astridx - close - 24 Jun 2018

Add a Comment

Login with GitHub to post a comment