0
Fork 0
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:
Matthew Borden 2015-01-04 08:22:32 +00:00
parent 8e8874b554
commit 36d8b3b3fc
6 changed files with 96 additions and 3 deletions

View file

@ -63,6 +63,10 @@
<span id="makeLink" class="prompt">Link</span> <span id="makeLink" class="prompt">Link</span>
</p> </p>
<p> <p>
<span id="makeCodeSnippet">Make Code Snippet</span>
<span id="removeCodeSnippet">Remove Code Snippet</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<span id="increaseQuoteLevel">Quote</span> <span id="increaseQuoteLevel">Quote</span>
<span id="decreaseQuoteLevel">Dequote</span> <span id="decreaseQuoteLevel">Dequote</span>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

View file

@ -2,7 +2,7 @@
all: install build all: install build
intall: install:
npm install npm install
clean: clean:

View file

@ -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. Decreases by 1 the nesting level of any at-least-partially selected blocks which are part of a list.
Returns self (the Squire instance). 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).

View file

@ -3417,6 +3417,44 @@ proto.setTextDirection = function ( direction ) {
return this.focus(); 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.increaseQuoteLevel = command( 'modifyBlocks', increaseBlockQuoteLevel );
proto.decreaseQuoteLevel = command( 'modifyBlocks', decreaseBlockQuoteLevel ); proto.decreaseQuoteLevel = command( 'modifyBlocks', decreaseBlockQuoteLevel );

File diff suppressed because one or more lines are too long

View file

@ -2339,6 +2339,44 @@ proto.setTextDirection = function ( direction ) {
return this.focus(); 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.increaseQuoteLevel = command( 'modifyBlocks', increaseBlockQuoteLevel );
proto.decreaseQuoteLevel = command( 'modifyBlocks', decreaseBlockQuoteLevel ); proto.decreaseQuoteLevel = command( 'modifyBlocks', decreaseBlockQuoteLevel );