Skip to content

Commit

Permalink
feat(bindgen): support typescript BinaryFile interface type
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Aug 2, 2023
1 parent 085c8d6 commit f6f988f
Show file tree
Hide file tree
Showing 19 changed files with 707 additions and 526 deletions.
2 changes: 1 addition & 1 deletion cypress/e2e/pipeline/runPipeline.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Click. Perfect success.
})


it('writes and reads itk.Image\'s in the Emscripten filesystem', () => {
it('writes and reads itk.Image\'s with Emscripten', () => {
cy.window().then(async (win) => {
const itk = win.itk

Expand Down
2 changes: 2 additions & 0 deletions src/bindgen/typescript/demo/all-demo-types-supported.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Evenutally we will support them all
const demoSupportedInputTypes = new Set([
'INPUT_TEXT_FILE',
'INPUT_TEXT_FILE:FILE',
'INPUT_TEXT_STREAM',
'INPUT_BINARY_FILE:FILE',
Expand All @@ -11,6 +12,7 @@ const demoSupportedInputTypes = new Set([
'INPUT_MESH',
])
const demoSupportedOutputTypes = new Set([
'OUTPUT_TEXT_FILE',
'OUTPUT_TEXT_FILE:FILE',
'OUTPUT_TEXT_STREAM',
'OUTPUT_BINARY_FILE:FILE',
Expand Down
2 changes: 2 additions & 0 deletions src/bindgen/typescript/demo/input-parameters-demo-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ function inputParametersDemoHtml(prefix, indent, parameter, required, useCamelCa
const requiredAttr = required ? 'required ' : ''
const label = useCamelCase ? camelCase(parameter.name) : snakeCase(parameter.name)
switch(parameter.type) {
case 'INPUT_TEXT_FILE':
case 'INPUT_TEXT_FILE:FILE':
case 'INPUT_TEXT_STREAM':
result += `${prefix}${indent}<sl-input ${requiredAttr}name="${parameter.name}" type="text" label="${label}" help-text="${parameter.description}"></sl-input>\n`
result += `${prefix}${indent}<label for="${parameter.name}-file"><sl-button variant="primary" outline onclick="this.parentElement.nextElementSibling.click()">Upload</sp-button></label><input type="file" name="${parameter.name}-file" style="display: none"/>\n`
result += `<br /><br />\n`
break
case 'INPUT_BINARY_FILE':
case 'INPUT_BINARY_FILE:FILE':
case 'INPUT_BINARY_STREAM':
result += `${prefix}${indent}<sl-input ${requiredAttr}name="${parameter.name}" type="text" label="${label}" help-text="${parameter.description}" disabled></sl-input>\n`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ function inputParametersDemoTypeScript(functionName, indent, parameter, required
const parameterName = camelCase(parameter.name)
const inputIdentifier = `${parameterName}Element`
switch(parameter.type) {
case 'INPUT_TEXT_FILE':
case 'INPUT_TEXT_FILE:FILE':
case 'INPUT_TEXT_STREAM':
result += `${indent}const ${inputIdentifier} = document.querySelector('#${functionName}Inputs input[name=${parameter.name}-file]')\n`
Expand All @@ -20,6 +21,7 @@ function inputParametersDemoTypeScript(functionName, indent, parameter, required
result += `${indent}${indent}})\n`
result += `${indent}})\n\n`
break
case 'INPUT_BINARY_FILE':
case 'INPUT_BINARY_FILE:FILE':
case 'INPUT_BINARY_STREAM':
result += `${indent}const ${inputIdentifier} = document.querySelector('#${functionName}Inputs input[name=${parameter.name}-file]')\n`
Expand Down
2 changes: 2 additions & 0 deletions src/bindgen/typescript/demo/output-demo-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import camelCase from '../../camel-case.js'
function outputDemoHtml(prefix, indent, parameter) {
let result = ''
switch(parameter.type) {
case 'OUTPUT_TEXT_FILE':
case 'OUTPUT_TEXT_FILE:FILE':
case 'OUTPUT_TEXT_STREAM':
result += `${prefix}${indent}<sl-textarea resize="auto" filled disabled name="${parameter.name}" label="${camelCase(parameter.name)}" help-text="${parameter.description}"><sl-skeleton effect="none"></sl-skeleton></sl-textarea>\n`
result += `${prefix}${indent}<sl-button variant="neutral" outline name="${parameter.name}-download" disabled>Download</sl-button>\n`
result += `<br /><br />\n`
break
case 'OUTPUT_BINARY_FILE':
case 'OUTPUT_BINARY_FILE:FILE':
case 'OUTPUT_BINARY_STREAM':
result += `${prefix}${indent}<sl-textarea resize="auto" filled disabled name="${parameter.name}" label="${camelCase(parameter.name)}" help-text="${parameter.description}"><sl-skeleton effect="none"></sl-skeleton></sl-textarea>\n`
Expand Down
14 changes: 10 additions & 4 deletions src/bindgen/typescript/demo/output-demo-run-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ function outputDemoRunTypeScript(functionName, prefix, indent, parameter) {
let result = `\n${prefix}${indent}model.outputs.set("${parameterName}", ${parameterName})\n`

switch(parameter.type) {
case 'OUTPUT_TEXT_FILE':
case 'OUTPUT_TEXT_FILE:FILE':
case 'OUTPUT_TEXT_STREAM':
case 'OUTPUT_TEXT_STREAM': {
result += `${prefix}${indent}${parameterName}OutputDownload.variant = "success"\n`
result += `${prefix}${indent}${parameterName}OutputDownload.disabled = false\n`
result += `${prefix}${indent}const ${parameterName}Output = document.querySelector('#${functionName}Outputs sl-textarea[name=${parameter.name}]')\n`
result += `${prefix}${indent}${parameterName}Output.value = ${parameterName}.substring(0, 1024).toString() + ' ...'\n`
const textDataProp = parameter.type.includes('FILE') ? '.data' : ''
result += `${prefix}${indent}${parameterName}Output.value = ${parameterName}${textDataProp}.substring(0, 1024).toString() + ' ...'\n`
result += `${prefix}${indent}${parameterName}Output.disabled = false\n`
}
break
case 'OUTPUT_BINARY_FILE':
case 'OUTPUT_BINARY_FILE:FILE':
case 'OUTPUT_BINARY_STREAM':
case 'OUTPUT_BINARY_STREAM': {
result += `${prefix}${indent}${parameterName}OutputDownload.variant = "success"\n`
result += `${prefix}${indent}${parameterName}OutputDownload.disabled = false\n`
result += `${prefix}${indent}const ${parameterName}Output = document.querySelector('#${functionName}Outputs sl-textarea[name=${parameter.name}]')\n`
result += `${prefix}${indent}${parameterName}Output.value = ${parameterName}.subarray(0, 1024).toString() + ' ...'\n`
const binaryDataProp = parameter.type.includes('FILE') ? '.data' : ''
result += `${prefix}${indent}${parameterName}Output.value = ${parameterName}${binaryDataProp}.subarray(0, 1024).toString() + ' ...'\n`
result += `${prefix}${indent}${parameterName}Output.disabled = false\n`
}
break
// case 'TEXT':
// result += `${prefix}${indent}<sl-textarea disabled name="${parameter.name}" label="${camelCase(parameter.name)}" help-text="${parameter.description}"></sl-textarea>\n`
Expand Down
16 changes: 12 additions & 4 deletions src/bindgen/typescript/demo/output-demo-typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@ function outputDemoTypeScript(functionName, prefix, indent, parameter) {
let result = '\n'

switch(parameter.type) {
case 'OUTPUT_TEXT_FILE':
case 'OUTPUT_TEXT_FILE:FILE':
case 'OUTPUT_TEXT_STREAM':
case 'OUTPUT_TEXT_STREAM': {
result += `${prefix}${indent}const ${parameterName}OutputDownload = document.querySelector('#${functionName}Outputs sl-button[name=${parameter.name}-download]')\n`
result += `${prefix}${indent}${parameterName}OutputDownload.addEventListener('click', (event) => {\n`
result += `${prefix}${indent}${indent}event.preventDefault()\n`
result += `${prefix}${indent}${indent}event.stopPropagation()\n`
result += `${prefix}${indent}${indent}if (model.outputs.has("${parameterName}")) {\n`
result += `${prefix}${indent}${indent}${indent}globalThis.downloadFile(new TextEncoder().encode(model.outputs.get("${parameterName}")), "${parameterName}.txt")\n`
const textDataProp = parameter.type.includes('FILE') ? '.data' : ''
const downloadFileName = parameter.type.includes('FILE') ? `model.outputs.get("${parameterName}").path`: `"${parameterName}.txt"`
result += `${prefix}${indent}${indent}${indent}globalThis.downloadFile(new TextEncoder().encode(model.outputs.get("${parameterName}")${textDataProp}), ${downloadFileName})\n`
result += `${prefix}${indent}${indent}}\n`
result += `${prefix}${indent}})\n`
}
break
case 'OUTPUT_BINARY_FILE':
case 'OUTPUT_BINARY_FILE:FILE':
case 'OUTPUT_BINARY_STREAM':
case 'OUTPUT_BINARY_STREAM': {
result += `${prefix}${indent}const ${parameterName}OutputDownload = document.querySelector('#${functionName}Outputs sl-button[name=${parameter.name}-download]')\n`
result += `${prefix}${indent}${parameterName}OutputDownload.addEventListener('click', (event) => {\n`
result += `${prefix}${indent}${indent}event.preventDefault()\n`
result += `${prefix}${indent}${indent}event.stopPropagation()\n`
result += `${prefix}${indent}${indent}if (model.outputs.has("${parameterName}")) {\n`
result += `${prefix}${indent}${indent}${indent}globalThis.downloadFile(model.outputs.get("${parameterName}"), "${parameterName}.bin")\n`
const binaryDataProp = parameter.type.includes('FILE') ? '.data' : ''
const downloadFileName = parameter.type.includes('FILE') ? `model.outputs.get("${parameterName}").path`: `"${parameterName}.bin"`
result += `${prefix}${indent}${indent}${indent}globalThis.downloadFile(model.outputs.get("${parameterName}")${binaryDataProp}, ${downloadFileName})\n`
result += `${prefix}${indent}${indent}}\n`
result += `${prefix}${indent}})\n`
}
break
// case 'TEXT':
// result += `${prefix}${indent}<sl-textarea disabled name="${parameter.name}" label="${camelCase(parameter.name)}" help-text="${parameter.description}"></sl-textarea>\n`
Expand Down
Loading

0 comments on commit f6f988f

Please sign in to comment.