hyperRTE

creators

various functions used to create ui component.

createDropDown(autoclose)

creates a drop down that closes when clicked outside.
args:

returns [dropDown, open, close].

addInput(editor, name, definition)

add a button that open a drop down with input.
args:

any additional property is assign to the input element.

example

add an input for inserting html.

addInput(editor, 'insert html', {
	title: 'insert html',
	icon: '<span>insert html</span>',
	action: (str) => $edit.insert(str, 'html')
})

variations

addInputs(editor, name, definition)

add a button that open a drop down with multiple inputs.
args:

example

add inputs for inserting image.

addInputs(editor, 'insert image', {
	title: 'insert image',
	icon: '<span>insert image</span>',
	inputs: [
		{type: 'text', placeholder: 'url'},
		{type: 'number', placeholder: 'height'},
		{type: 'number', placeholder: 'width'}
	],
	action: (url, height, width) => {
		var img = document.createElement('img');
		img.src = url;
		img.width = width + 'px';
		img.height = height + 'px';
		$edit.insert(img, 'node')
	}
})

addList(editor, name, definition)

add a button that open a drop down with a list of items.
args:

example

add list for adding headings.

addList(editor, 'heading', {
	title: 'heading',
	icon: '<span>heading</span>',
	data: [1, 2, 3, 4, 5, 6],
	item: (size) => {
		var el = document.createElement('h' + size);
		el.innerText = 'size ' + size;
		return el
	},
	action: (size) => $edit.heading(size)
})

createDialog(autoclose)

creates a dialog that closes when clicked outside.
args:

returns [dialog, open, close].

addPanel

add a button that hide the main editor view and show a panel. args:

example

add an panel to show source.

addPanel(editor, 'source', {
	title: 'view source',
	icon: '<span>source</span>',
	creator: (editor, back) => {
		var panel = document.createElement('div');
		var backButton = document.createElement('button');
		backButton.innerText = 'back';
		backButton.addEventListener('click', back);
		var sourceEl = document.createElement('div');
		panel.append(backButton, sourceEl);
		return [panel, () => sourceEl.innerText = editor.content]
	}
})

createMenu(editor, items)

create a menu that can be opened and closed.
args:

addMenu(editor, name, definition)

add a button that opens a menu.
args:

example

creates a button that open a menu of text snippets:

addMenu(editor, 'snippets', {
	title: 'insert snippets',
	icon: '<span>snippets</span>',
	'hello world': () => editor.doAction('snippet', () => $edit.insert('hello world', 'text')),
	'js is good': () => editor.doAction('snippet', () => $edit.insert('js is good', 'text')),
	'hyper is better': () => editor.doAction('snippet', () => $edit.insert('hyper is better', 'text'))
})