0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-05 06:10:07 -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
parent 02df8f9ed4
commit 02ff9fe985
4 changed files with 60 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.
### 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 has been made, it will return an empty object.
### getSelection
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,33 @@ proto.hasFormat = function ( tag, attributes, range ) {
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 ) {
if ( !range && !( range = this.getSelection() ) ) {
return {};
}
var element, fontInfo = {};
if ( range.collapsed ) {
element = range.commonAncestorContainer;
if (element.nodeType === TEXT_NODE) {
element = element.parentNode;
}
while ( !fontInfo.family && !fontInfo.size && element && element.nodeName !== 'BODY' ) {
if ( element.style.fontFamily ) {
fontInfo.family = element.style.fontFamily;
}
if ( element.style.fontSize ) {
fontInfo.size = element.style.fontSize;
}
element = element.parentNode;
}
}
return fontInfo;
};
proto._addFormat = function ( tag, attributes, range ) {
// If the range is collapsed we simply insert the node by wrapping
// it round the range and focus it.

File diff suppressed because one or more lines are too long

View file

@ -710,6 +710,33 @@ proto.hasFormat = function ( tag, attributes, range ) {
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 ) {
if ( !range && !( range = this.getSelection() ) ) {
return {};
}
var element, fontInfo = {};
if ( range.collapsed ) {
element = range.commonAncestorContainer;
if (element.nodeType === TEXT_NODE) {
element = element.parentNode;
}
while ( !fontInfo.family && !fontInfo.size && element && element.nodeName !== 'BODY' ) {
if ( element.style.fontFamily ) {
fontInfo.family = element.style.fontFamily;
}
if ( element.style.fontSize ) {
fontInfo.size = element.style.fontSize;
}
element = element.parentNode;
}
}
return fontInfo;
};
proto._addFormat = function ( tag, attributes, range ) {
// If the range is collapsed we simply insert the node by wrapping
// it round the range and focus it.