spec/es5TestBundle.js | spec/es5TestBundle.js | ||||
.env | .env | ||||
dist | dist | ||||
index.html | |||||
index.js |
div.style.position = 'absolute' | div.style.position = 'absolute' | ||||
div.style.top = 0 | div.style.top = 0 | ||||
div.style.left = 0 | div.style.left = 0 | ||||
} catch (e) {} | |||||
} catch (e) { | |||||
// | |||||
} | |||||
div.appendChild(fixtures()) | div.appendChild(fixtures()) | ||||
body.appendChild(div) | body.appendChild(div) | ||||
div.style.position = 'absolute' | div.style.position = 'absolute' | ||||
div.style.top = 0 | div.style.top = 0 | ||||
div.style.left = 0 | div.style.left = 0 | ||||
} catch (e) {} | |||||
} catch (e) { | |||||
// | |||||
} | |||||
body.appendChild(div) | body.appendChild(div) | ||||
} | } | ||||
expect(text.text()).toBe('Hello World\nHow is it\ngoing') | expect(text.text()).toBe('Hello World\nHow is it\ngoing') | ||||
}) | }) | ||||
it('returns the correct text with newlines and skips textPaths', () => { | |||||
it('returns the correct text with newlines and skips textPaths and descriptive elements', () => { | |||||
const path = new Path() | const path = new Path() | ||||
const text = new Text() | const text = new Text() | ||||
const textPath = text.text('Hello World\nHow is it\ngoing').path(path) | const textPath = text.text('Hello World\nHow is it\ngoing').path(path) | ||||
textPath.children().addTo(text) | textPath.children().addTo(text) | ||||
text.add(new TextPath(), 3) | text.add(new TextPath(), 3) | ||||
text.add(SVG('<title>MyText</title>')) | |||||
expect(text.text()).toBe('Hello World\nHow is it\ngoing') | expect(text.text()).toBe('Hello World\nHow is it\ngoing') | ||||
}) | }) | ||||
t.tspan('Hello World').newLine() | t.tspan('Hello World').newLine() | ||||
t.tspan('How is it').newLine() | t.tspan('How is it').newLine() | ||||
t.tspan('going').newLine() | t.tspan('going').newLine() | ||||
t.add('<title>My Text</title>') | |||||
}) | }) | ||||
const dy = text.get(1).dy() | const dy = text.get(1).dy() |
import Shape from './Shape.js' | import Shape from './Shape.js' | ||||
import { globals } from '../utils/window.js' | import { globals } from '../utils/window.js' | ||||
import * as textable from '../modules/core/textable.js' | import * as textable from '../modules/core/textable.js' | ||||
import { isDescriptive } from '../utils/utils.js' | |||||
export default class Text extends Shape { | export default class Text extends Shape { | ||||
// Initialize node | // Initialize node | ||||
const leading = this.dom.leading | const leading = this.dom.leading | ||||
this.each(function (i) { | this.each(function (i) { | ||||
if (isDescriptive(this.node)) return | |||||
const fontSize = globals.window | const fontSize = globals.window | ||||
.getComputedStyle(this.node) | .getComputedStyle(this.node) | ||||
.getPropertyValue('font-size') | .getPropertyValue('font-size') | ||||
for (let i = 0, len = children.length; i < len; ++i) { | for (let i = 0, len = children.length; i < len; ++i) { | ||||
// skip textPaths - they are no lines | // skip textPaths - they are no lines | ||||
if (children[i].nodeName === 'textPath') { | |||||
if (i === 0) firstLine = 1 | |||||
if (children[i].nodeName === 'textPath' || isDescriptive(children[i])) { | |||||
if (i === 0) firstLine = i + 1 | |||||
continue | continue | ||||
} | } | ||||
import { getOrigin } from '../../utils/utils.js' | |||||
import { getOrigin, isDescriptive } from '../../utils/utils.js' | |||||
import { delimiter, transforms } from '../core/regex.js' | import { delimiter, transforms } from '../core/regex.js' | ||||
import { registerMethods } from '../../utils/methods.js' | import { registerMethods } from '../../utils/methods.js' | ||||
import Matrix from '../../types/Matrix.js' | import Matrix from '../../types/Matrix.js' | ||||
// add an element to another parent without changing the visual representation on the screen | // add an element to another parent without changing the visual representation on the screen | ||||
export function toParent(parent, i) { | export function toParent(parent, i) { | ||||
if (this === parent) return this | if (this === parent) return this | ||||
if (isDescriptive(this.node)) return this.addTo(parent, i) | |||||
const ctm = this.screenCTM() | const ctm = this.screenCTM() | ||||
const pCtm = parent.screenCTM().inverse() | const pCtm = parent.screenCTM().inverse() | ||||
// Return the origin as it is if it wasn't a string | // Return the origin as it is if it wasn't a string | ||||
return [ox, oy] | return [ox, oy] | ||||
} | } | ||||
const descriptiveElements = new Set(['desc', 'metadata', 'title']) | |||||
export const isDescriptive = (element) => | |||||
descriptiveElements.has(element.nodeName) |