0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Add colour/background-colour to result of getFontInfo

Resolves #158
This commit is contained in:
Neil Jenkins 2015-12-03 18:14:49 +11:00
parent 0d1f1cf44b
commit df25d6d596
4 changed files with 39 additions and 15 deletions

View file

@ -181,7 +181,7 @@ Returns the path through the DOM tree from the `<body>` element to the current c
### 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.
Returns an object containing the active font family, size, colour and background colour for the the current cursor position, if any are set. The property names are respectively `family`, `size`, `color` and `backgroundColor`. It looks at style attributes to detect this, so will not detect `<FONT>` tags or non-inline styles. If a selection across multiple elements has been made, it will return an empty object.
### getSelection

View file

@ -2866,10 +2866,13 @@ proto.hasFormat = function ( tag, attributes, range ) {
// holding the cursor. If there's a selection, returns an empty object.
proto.getFontInfo = function ( range ) {
var fontInfo = {
family: undefined,
size: undefined
},
element, style;
color: undefined,
backgroundColor: undefined,
family: undefined,
size: undefined
};
var seenAttributes = 0;
var element, style;
if ( !range && !( range = this.getSelection() ) ) {
return fontInfo;
@ -2880,13 +2883,22 @@ proto.getFontInfo = function ( range ) {
if ( element.nodeType === TEXT_NODE ) {
element = element.parentNode;
}
while ( !( fontInfo.family && fontInfo.size ) &&
element && ( style = element.style ) ) {
while ( seenAttributes < 4 && element && ( style = element.style ) ) {
if ( !fontInfo.color ) {
fontInfo.color = style.color;
seenAttributes += 1;
}
if ( !fontInfo.backgroundColor ) {
fontInfo.backgroundColor = style.backgroundColor;
seenAttributes += 1;
}
if ( !fontInfo.family ) {
fontInfo.family = style.fontFamily;
seenAttributes += 1;
}
if ( !fontInfo.size ) {
fontInfo.size = style.fontSize;
seenAttributes += 1;
}
element = element.parentNode;
}

File diff suppressed because one or more lines are too long

View file

@ -734,10 +734,13 @@ proto.hasFormat = function ( tag, attributes, range ) {
// holding the cursor. If there's a selection, returns an empty object.
proto.getFontInfo = function ( range ) {
var fontInfo = {
family: undefined,
size: undefined
},
element, style;
color: undefined,
backgroundColor: undefined,
family: undefined,
size: undefined
};
var seenAttributes = 0;
var element, style;
if ( !range && !( range = this.getSelection() ) ) {
return fontInfo;
@ -748,13 +751,22 @@ proto.getFontInfo = function ( range ) {
if ( element.nodeType === TEXT_NODE ) {
element = element.parentNode;
}
while ( !( fontInfo.family && fontInfo.size ) &&
element && ( style = element.style ) ) {
while ( seenAttributes < 4 && element && ( style = element.style ) ) {
if ( !fontInfo.color ) {
fontInfo.color = style.color;
seenAttributes += 1;
}
if ( !fontInfo.backgroundColor ) {
fontInfo.backgroundColor = style.backgroundColor;
seenAttributes += 1;
}
if ( !fontInfo.family ) {
fontInfo.family = style.fontFamily;
seenAttributes += 1;
}
if ( !fontInfo.size ) {
fontInfo.size = style.fontSize;
seenAttributes += 1;
}
element = element.parentNode;
}