Skip to content

Commit

Permalink
fixed root(), textPath(), text.path() and path.text() and rem…
Browse files Browse the repository at this point in the history
…oved font-family and size from the defaults list
  • Loading branch information
Fuzzyma committed Jan 14, 2019
1 parent aec779c commit 92b48d2
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ The document follows the conventions described in [“Keep a CHANGELOG”](http:
## [3.0.10] - 2018-01-14

### Fixed
- fixed `textPath()` and `path().text()`
- fixed `textPath()`, `path().text()` and `text().path()`
- fixed `root()` method
- fixed default values returned by `attr`. Can be missleading if present.

### Added
- added `findOne()` for better performance

## [3.0.9] - 2018-01-14

Expand Down
15 changes: 14 additions & 1 deletion spec/spec/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,20 @@ describe('Selector', function() {
, e4 = draw.rect(100, 100).addClass('unselectable-element')
, e5 = group.rect(100, 100).addClass('selectable-element')

expect(group.find('rect.selectable-element').valueOf()).toEqual([e3, e5])
expect(group.find('rect.selectable-element')).toEqual([e3, e5])
})
})

describe('Parent#findOne()', function() {
it('gets all elements with a given class name inside a given element', function() {
var group = draw.group()
, e1 = draw.rect(100, 100).addClass('selectable-element')
, e2 = draw.rect(100, 100).addClass('unselectable-element')
, e3 = group.rect(100, 100).addClass('selectable-element')
, e4 = draw.rect(100, 100).addClass('unselectable-element')
, e5 = group.rect(100, 100).addClass('selectable-element')

expect(group.findOne('rect.selectable-element')).toBe(e3)
})
})

Expand Down
4 changes: 2 additions & 2 deletions src/elements/Dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
create,
register
} from '../utils/adopter.js'
import { find } from '../modules/core/selector.js'
import { find, findOne } from '../modules/core/selector.js'
import { globals } from '../utils/window.js'
import { map } from '../utils/utils.js'
import { ns } from '../modules/core/namespaces.js'
Expand Down Expand Up @@ -313,5 +313,5 @@ export default class Dom extends EventTarget {
}
}

extend(Dom, { attr, find })
extend(Dom, { attr, find, findOne })
register(Dom)
4 changes: 1 addition & 3 deletions src/elements/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import Dom from './Dom.js'
import List from '../types/List.js'
import SVGNumber from '../types/SVGNumber.js'

const Svg = getClass(root)

export default class Element extends Dom {
constructor (node, attrs) {
super(node, attrs)
Expand Down Expand Up @@ -67,7 +65,7 @@ export default class Element extends Dom {

// Get parent document
root () {
let p = this.parent(Svg)
let p = this.parent(getClass(root))
return p && p.root()
}

Expand Down
47 changes: 20 additions & 27 deletions src/elements/TextPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,43 @@ export default class TextPath extends Text {
registerMethods({
Container: {
textPath: wrapWithAttrCheck(function (text, path) {
// Convert to instance if needed
if (!(path instanceof Path)) {
path = this.defs().path(path)
// Convert text to instance if needed
if (!(text instanceof Text)) {
text = this.text(text)
}

// Create textPath
const textPath = path.text(text)

// Move text to correct container
textPath.parent().addTo(this)

return textPath
return text.path(path)
})
},
Text: {
// Create path for text to run on
path: wrapWithAttrCheck(function (track) {
var path = new TextPath()
path: wrapWithAttrCheck(function (track, importNodes = true) {
var textPath = new TextPath()

// if track is a path, reuse it
if (!(track instanceof Path)) {
// create path element
track = this.root().defs().path(track)
track = this.defs().path(track)
}

// link textPath to path and add content
path.attr('href', '#' + track, xlink)
textPath.attr('href', '#' + track, xlink)

// Transplant all nodes from text to textPath
let node
if (importNodes) {
while ((node = this.node.firstChild)) {
textPath.node.appendChild(node)
}
}

// add textPath element as child node and return textPath
return this.put(path)
return this.put(textPath)
}),

// Get the textPath children
textPath () {
return this.find('textPath')[0]
return this.findOne('textPath')
}
},
Path: {
Expand All @@ -85,17 +87,8 @@ registerMethods({
text = new Text().addTo(this.parent()).text(text)
}

// Create textPath from text and path
const textPath = text.path(this)
textPath.remove()

// Transplant all nodes from text to textPath
let node
while ((node = text.node.firstChild)) {
textPath.node.appendChild(node)
}

return textPath.addTo(text)
// Create textPath from text and path and return
return text.path(this)
}),

targets () {
Expand Down
2 changes: 0 additions & 2 deletions src/modules/core/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,5 @@ export let attrs = {
'stop-color': '#000000',

// text
'font-size': 16,
'font-family': 'Helvetica, Arial, sans-serif',
'text-anchor': 'start'
}
4 changes: 4 additions & 0 deletions src/modules/core/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export default function baseFind (query, parent) {
export function find (query) {
return baseFind(query, this.node)
}

export function findOne (query) {
return adopt(this.node.querySelector(query))
}

0 comments on commit 92b48d2

Please sign in to comment.