mirror of
https://github.com/fastmail/Squire.git
synced 2025-01-03 05:00:13 -05:00
Add makeCodeSnippet & removeCodeSnippet methods + documentation.
Adds the ability to wrap text as code. The entire selection is wrapped in a <pre> tag. Each line is a <code> tag inside a <div> tag. Lines are located at the path BODY > PRE > DIV > CODE.
This commit is contained in:
parent
8e8874b554
commit
36d8b3b3fc
6 changed files with 96 additions and 3 deletions
|
@ -63,6 +63,10 @@
|
|||
<span id="makeLink" class="prompt">Link</span>
|
||||
</p>
|
||||
<p>
|
||||
|
||||
<span id="makeCodeSnippet">Make Code Snippet</span>
|
||||
<span id="removeCodeSnippet">Remove Code Snippet</span>
|
||||
|
||||
<span id="increaseQuoteLevel">Quote</span>
|
||||
<span id="decreaseQuoteLevel">Dequote</span>
|
||||
|
||||
|
|
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@
|
|||
|
||||
all: install build
|
||||
|
||||
intall:
|
||||
install:
|
||||
npm install
|
||||
|
||||
clean:
|
||||
|
|
13
README.md
13
README.md
|
@ -352,3 +352,16 @@ Returns self (the Squire instance).
|
|||
Decreases by 1 the nesting level of any at-least-partially selected blocks which are part of a list.
|
||||
|
||||
Returns self (the Squire instance).
|
||||
|
||||
### makeCodeSnippet ###
|
||||
|
||||
Makes the currently selected text a code snippet. The entire selection is wrapped in a `<pre>` tag. Each line is wrapped in a `<code>` tag within a `<div>` tag. If no text is selected, the input mode is set to a code snippet.
|
||||
Lines are located at the path `PRE > DIV > CODE`.
|
||||
|
||||
Returns self (the Squire instance).
|
||||
|
||||
### removeCodeSnippet ###
|
||||
|
||||
Removes any `<pre>` or `<code>` tags, that are currently (at least partially) selected.
|
||||
|
||||
Returns self (the Squire instance).
|
|
@ -3417,6 +3417,44 @@ proto.setTextDirection = function ( direction ) {
|
|||
return this.focus();
|
||||
};
|
||||
|
||||
proto.makeCodeSnippet = function () {
|
||||
this.changeFormat({
|
||||
tag: 'CODE'
|
||||
}, null, this.getSelection(), true );
|
||||
|
||||
this.modifyBlocks(function ( frag ) {
|
||||
if (frag.querySelectorAll( 'pre' ).length === 0) {
|
||||
return this.createElement( 'PRE', [
|
||||
frag
|
||||
]);
|
||||
} else {
|
||||
// Return original fragment, prevent adding more than one pre tag.
|
||||
return frag;
|
||||
}
|
||||
});
|
||||
|
||||
return this.focus();
|
||||
};
|
||||
|
||||
proto.removeCodeSnippet = function () {
|
||||
this.changeFormat( null, {
|
||||
tag: 'CODE'
|
||||
}, this.getSelection(), true );
|
||||
|
||||
this.modifyBlocks(function ( frag ) {
|
||||
var pres = frag.querySelectorAll( 'pre' );
|
||||
Array.prototype.filter.call( pres, function ( el ) {
|
||||
return !getNearest( el.parentNode, 'PRE' );
|
||||
}).forEach( function ( el ) {
|
||||
replaceWith( el, empty( el ) );
|
||||
});
|
||||
|
||||
return frag;
|
||||
});
|
||||
|
||||
return this.focus();
|
||||
};
|
||||
|
||||
proto.increaseQuoteLevel = command( 'modifyBlocks', increaseBlockQuoteLevel );
|
||||
proto.decreaseQuoteLevel = command( 'modifyBlocks', decreaseBlockQuoteLevel );
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -2339,6 +2339,44 @@ proto.setTextDirection = function ( direction ) {
|
|||
return this.focus();
|
||||
};
|
||||
|
||||
proto.makeCodeSnippet = function () {
|
||||
this.changeFormat({
|
||||
tag: 'CODE'
|
||||
}, null, this.getSelection(), true );
|
||||
|
||||
this.modifyBlocks(function ( frag ) {
|
||||
if (frag.querySelectorAll( 'pre' ).length === 0) {
|
||||
return this.createElement( 'PRE', [
|
||||
frag
|
||||
]);
|
||||
} else {
|
||||
// Return original fragment, prevent adding more than one pre tag.
|
||||
return frag;
|
||||
}
|
||||
});
|
||||
|
||||
return this.focus();
|
||||
};
|
||||
|
||||
proto.removeCodeSnippet = function () {
|
||||
this.changeFormat( null, {
|
||||
tag: 'CODE'
|
||||
}, this.getSelection(), true );
|
||||
|
||||
this.modifyBlocks(function ( frag ) {
|
||||
var pres = frag.querySelectorAll( 'pre' );
|
||||
Array.prototype.filter.call( pres, function ( el ) {
|
||||
return !getNearest( el.parentNode, 'PRE' );
|
||||
}).forEach( function ( el ) {
|
||||
replaceWith( el, empty( el ) );
|
||||
});
|
||||
|
||||
return frag;
|
||||
});
|
||||
|
||||
return this.focus();
|
||||
};
|
||||
|
||||
proto.increaseQuoteLevel = command( 'modifyBlocks', increaseBlockQuoteLevel );
|
||||
proto.decreaseQuoteLevel = command( 'modifyBlocks', decreaseBlockQuoteLevel );
|
||||
|
||||
|
|
Loading…
Reference in a new issue