mirror of
https://github.com/fastmail/Squire.git
synced 2024-12-22 07:13:08 -05:00
Add the ability to insert an HTML fragment at the cursor location.
This commit is contained in:
parent
73ca65edb5
commit
6fdb288e00
2 changed files with 53 additions and 0 deletions
10
README.md
10
README.md
|
@ -136,6 +136,16 @@ The method takes one argument:
|
|||
|
||||
Returns a reference to the newly inserted image element.
|
||||
|
||||
### insertHTML
|
||||
|
||||
Inserts an HTML fragment at the current cursor location. The value supplied should not contain `<body>` tags or anything outside of that.
|
||||
|
||||
The method takes one argument:
|
||||
|
||||
* **html**: The html to insert.
|
||||
|
||||
Returns self (the Squire instance).
|
||||
|
||||
### getPath
|
||||
|
||||
Returns the path through the DOM tree from the `<body>` element to the current current cursor position. This is a string consisting of the tag, id and class names in CSS format. For example `BODY>BLOCKQUOTE>DIV#id>STRONG>SPAN.font>EM`. If a selection has been made, so different parts of the selection may have different paths, the value will be `(selection)`. The path is useful for efficiently determining the current formatting for bold, italic, underline etc, and thus determining button state. If a selection has been made, you can has the `hasFormat` method instead to get the current state for the properties you care about.
|
||||
|
|
|
@ -2263,6 +2263,49 @@ proto.insertImage = function ( src ) {
|
|||
return img;
|
||||
};
|
||||
|
||||
proto.insertHTML = function ( html ) {
|
||||
var self = this,
|
||||
range = this.getSelection(),
|
||||
frag = this._doc.createDocumentFragment(),
|
||||
div = this.createElement( 'DIV' );
|
||||
|
||||
// Parse HTML into DOM tree
|
||||
div.innerHTML = html;
|
||||
frag.appendChild( empty( div ) );
|
||||
|
||||
// Record undo checkpoint
|
||||
self._recordUndoState( range );
|
||||
self._getRangeAndRemoveBookmark( range );
|
||||
|
||||
|
||||
try {
|
||||
frag.normalize();
|
||||
addLinks( frag );
|
||||
cleanTree( frag, true );
|
||||
cleanupBRs( frag );
|
||||
removeEmptyInlines( frag );
|
||||
fixContainer( frag );
|
||||
|
||||
var node = frag;
|
||||
while ( node = getNextBlock( node ) ) {
|
||||
fixCursor( node );
|
||||
}
|
||||
|
||||
insertTreeFragmentIntoRange( range, frag );
|
||||
if ( !canObserveMutations ) {
|
||||
self._docWasChanged();
|
||||
}
|
||||
range.collapse( false );
|
||||
self._ensureBottomLine();
|
||||
|
||||
self.setSelection( range );
|
||||
self._updatePath( range, true );
|
||||
|
||||
} catch ( error ) {
|
||||
self.didError( error );
|
||||
}
|
||||
};
|
||||
|
||||
// --- Formatting ---
|
||||
|
||||
var command = function ( method, arg, arg2 ) {
|
||||
|
|
Loading…
Reference in a new issue