-
Notifications
You must be signed in to change notification settings - Fork 1.9k
MediumEditor Object API
- Initialization Functions
- Event Functions
- Selection Functions
- Editor Action Functions
- Helper Functions
Creating an instance of MediumEditor will:
- Convert all passed in elements into
contenteditable
elements. - For any
<textarea>
elements, hide the<textarea>
, create a new<div contenteditable=true>
element, and ensure the 2 elements remain sync'd. - Initialize any custom extensions or buttons passed in.
- Create any additional elements needed.
- Setup all event handling needed to monitor the editable elements.
Arguments
elements (String
| HTMLElement
| Array
):
-
String
: If passed as a string, this is used as a selector in a call todocument.querySelectorAll()
to find elements on the page. All results are stored in the internal list of elements. -
HTMLElement
: If passed as a single element, this will be the only element in the internal list of elements. -
Array
: If passed as anArray
ofHTMLElement
s, this will be used as the internal list of elements.
options (Object
):
Set of custom options used to initialize MediumEditor
.
tears down the editor if already setup, removing all DOM elements and event handlers
rebuilds the editor if it has already been destroyed, recreating DOM elements and attaching event handlers
attach a listener to a DOM event which will be detached when MediumEditor is deactivated
detach a listener to a DOM event that was attached via on()
Attaches a listener for the specified custom event name.
Arguments
-
name (
String
):
- Name of the event to listen to. See the list of built-in Custom Events below.
-
listener(data, editable) (
function
):
- Listener method that will be called whenever the custom event is triggered.
Arguments to listener
-
data (
Event
|object
) * For most custom events, this will be the browser's nativeEvent
object for the event that triggered the custom event to fire. * For some custom events, this will be an object containing information describing the event (depending on which custom event it is) -
editable (
HTMLElement
) * A reference to the contenteditable container element that this custom event corresponds to. This is especially useful for instances where one instance of MediumEditor contains multiple elements, or there are multiple instances of MediumEditor on the page. * For example, whenblur
fires, this argument will be the<div contenteditable=true></div>
element that is about to receive focus.
Detaches a custom event listener for the specified custom event name.
Arguments
-
name (
String
):
- Name of the event to detach the listener for.
-
listener (
function
):
- A reference to the listener to detach. This must be a match by-reference and not a copy.
NOTE
- Calling destroy() on the MediumEditor object will automatically remove all custom event listeners.
Manually triggers a custom event.
-
name (
String
):
- Name of the custom event to trigger.
-
data (
Event
|object
):
- Native
Event
object or custom data object to pass to all the listeners to this custom event.
-
editable (
HTMLElement
):
- The
<div contenteditable=true></div>
element to pass to all of the listeners to this custom event.
manually trigger an update of the toolbar and extensions based on the current selection
return a data representation of the selected text, which can be applied via importSelection()
restore the selection using a data representation of previously selected text (ie value returned by exportSelection())
returns an element if any contenteditable element monitored by MediumEditor currently has focused
get the parent contenteditable element that contains the current selection
restore the selection to what was selected the last time saveSelection() was called
internally store the set of selected text
expands the selection to contain all text within the focused contenteditable
change selection to be a specific element and update the toolbar to reflect the selection
stop the toolbar from updating to reflect the state of the selected text
put the toolbar back into its normal updating state
convert text to plaintext and replace current selection with result
creates a link via the native document.execCommand('createLink')
command
executes an built-in action via document.execCommand
replace the current selection with html
wrapper around the browser's built in document.queryCommandState(action)
for checking whether a specific action has already been applied to the selection.
delay any function from being executed by the amount of time passed as the delay option
get a reference to an extension with the specified name
returns a JSON object with elements contents