-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: add a utility to create DOM elements
- Loading branch information
Showing
11 changed files
with
341 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const id = '#waveform' | ||
|
||
describe('WaveSurfer Envelope plugin tests', () => { | ||
it('should render an envelope', () => { | ||
cy.visit('cypress/e2e/index.html') | ||
cy.window().then((win) => { | ||
return new Promise((resolve) => { | ||
win.wavesurfer = win.WaveSurfer.create({ | ||
container: id, | ||
height: 200, | ||
url: '../../examples/audio/demo.wav', | ||
plugins: [ | ||
win.Envelope.create({ | ||
volume: 0.8, | ||
lineColor: 'rgba(255, 0, 0, 0.5)', | ||
lineWidth: 4, | ||
dragPointSize: 20, | ||
dragLine: false, | ||
dragPointFill: 'rgba(0, 255, 255, 0.8)', | ||
dragPointStroke: 'rgba(0, 0, 0, 0.5)', | ||
|
||
points: [ | ||
{ time: 11.2, volume: 0.5 }, | ||
{ time: 15.5, volume: 0.8 }, | ||
], | ||
}), | ||
], | ||
}) | ||
|
||
win.wavesurfer.once('ready', () => { | ||
cy.get(id).matchImageSnapshot('envelope-basic') | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
it('should render an envelope and add a point', () => { | ||
cy.visit('cypress/e2e/index.html') | ||
cy.window().then((win) => { | ||
return new Promise((resolve) => { | ||
const envelopePlugin = win.Envelope.create({ | ||
volume: 0.5, | ||
lineColor: 'rgba(255, 0, 0, 0.5)', | ||
lineWidth: 10, | ||
dragPointSize: 12, | ||
dragLine: true, | ||
dragPointFill: 'rgba(0, 255, 255, 0.8)', | ||
dragPointStroke: 'rgba(0, 0, 0, 0.5)', | ||
|
||
points: [ | ||
{ time: 12.2, volume: 0.4 }, | ||
{ time: 16.5, volume: 0.9 }, | ||
], | ||
}) | ||
|
||
win.wavesurfer = win.WaveSurfer.create({ | ||
container: id, | ||
height: 200, | ||
url: '../../examples/audio/demo.wav', | ||
plugins: [envelopePlugin], | ||
}) | ||
|
||
envelopePlugin.addPoint({ id: 'new-point', time: 10.1, volume: 0.6 }) | ||
|
||
win.wavesurfer.once('ready', () => { | ||
cy.get(id).matchImageSnapshot('envelope-add-point') | ||
resolve() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
type TreeNode = { [key: string]: string | number | boolean | CSSStyleDeclaration | TreeNode | Node } & { | ||
xmlns?: string | ||
style?: Partial<CSSStyleDeclaration> | ||
textContent?: string | Node | ||
children?: TreeNode | ||
} | ||
|
||
function renderNode(tagName: string, content: TreeNode): HTMLElement | SVGElement { | ||
const element = content.xmlns | ||
? (document.createElementNS(content.xmlns, tagName) as SVGElement) | ||
: (document.createElement(tagName) as HTMLElement) | ||
|
||
for (const [key, value] of Object.entries(content)) { | ||
if (key === 'children') { | ||
for (const [key, value] of Object.entries(content)) { | ||
if (typeof value === 'string') { | ||
element.appendChild(document.createTextNode(value)) | ||
} else { | ||
element.appendChild(renderNode(key, value as TreeNode)) | ||
} | ||
} | ||
} else if (key === 'style') { | ||
Object.assign((element as HTMLElement).style, value) | ||
} else if (key === 'textContent') { | ||
element.textContent = value as string | ||
} else { | ||
element.setAttribute(key, value.toString()) | ||
} | ||
} | ||
|
||
return element | ||
} | ||
|
||
function render(tagName: string, content: TreeNode & { xmlns: string }, container?: Node): SVGElement | ||
function render(tagName: string, content?: TreeNode, container?: Node): HTMLElement | ||
function render(tagName: string, content?: TreeNode, container?: Node): HTMLElement | SVGElement { | ||
const el = renderNode(tagName, content || {}) | ||
container?.appendChild(el) | ||
return el | ||
} | ||
|
||
export default render |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.