0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Added getFontInfo() function, which extracts font-family and font-size styles.

This commit is contained in:
Gert K. Sønderby 2015-08-27 11:48:54 +02:00 committed by Neil Jenkins
parent 969dd6f177
commit 94f5a56255
4 changed files with 70 additions and 2 deletions

View file

@ -179,6 +179,10 @@ Returns self (the Squire instance).
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. 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.
### getFontInfo
Returns an object containing the font-family and font-size information for the element the cursor is in, if any was set. It uses style declarations to detect this, and so will not detect FONT tags. If a selection across multiple elements has been made, it will return an empty object.
### getSelection ### getSelection
Returns a [W3C Range object](https://developer.mozilla.org/en-US/docs/Web/API/Range) representing the current selection/cursor position. Returns a [W3C Range object](https://developer.mozilla.org/en-US/docs/Web/API/Range) representing the current selection/cursor position.

View file

@ -2835,6 +2835,38 @@ proto.hasFormat = function ( tag, attributes, range ) {
return seenNode; return seenNode;
}; };
// Extracts the font-family and font-size (if any) of the element
// holding the cursor. If there's a selection, returns an empty object.
proto.getFontInfo = function ( range ) {
var fontInfo = {
family: undefined,
size: undefined
},
element, style;
if ( !range && !( range = this.getSelection() ) ) {
return fontInfo;
}
element = range.commonAncestorContainer;
if ( range.collapsed || element.nodeType === TEXT_NODE ) {
if ( element.nodeType === TEXT_NODE ) {
element = element.parentNode;
}
while ( !( fontInfo.family && fontInfo.size ) &&
element && ( style = element.style ) ) {
if ( !fontInfo.family ) {
fontInfo.family = style.fontFamily;
}
if ( !fontInfo.size ) {
fontInfo.size = style.fontSize;
}
element = element.parentNode;
}
}
return fontInfo;
};
proto._addFormat = function ( tag, attributes, range ) { proto._addFormat = function ( tag, attributes, range ) {
// If the range is collapsed we simply insert the node by wrapping // If the range is collapsed we simply insert the node by wrapping
// it round the range and focus it. // it round the range and focus it.

File diff suppressed because one or more lines are too long

View file

@ -710,6 +710,38 @@ proto.hasFormat = function ( tag, attributes, range ) {
return seenNode; return seenNode;
}; };
// Extracts the font-family and font-size (if any) of the element
// holding the cursor. If there's a selection, returns an empty object.
proto.getFontInfo = function ( range ) {
var fontInfo = {
family: undefined,
size: undefined
},
element, style;
if ( !range && !( range = this.getSelection() ) ) {
return fontInfo;
}
element = range.commonAncestorContainer;
if ( range.collapsed || element.nodeType === TEXT_NODE ) {
if ( element.nodeType === TEXT_NODE ) {
element = element.parentNode;
}
while ( !( fontInfo.family && fontInfo.size ) &&
element && ( style = element.style ) ) {
if ( !fontInfo.family ) {
fontInfo.family = style.fontFamily;
}
if ( !fontInfo.size ) {
fontInfo.size = style.fontSize;
}
element = element.parentNode;
}
}
return fontInfo;
};
proto._addFormat = function ( tag, attributes, range ) { proto._addFormat = function ( tag, attributes, range ) {
// If the range is collapsed we simply insert the node by wrapping // If the range is collapsed we simply insert the node by wrapping
// it round the range and focus it. // it round the range and focus it.