diff --git a/app/controllers/templates_detect_fields_controller.rb b/app/controllers/templates_detect_fields_controller.rb index 56b06cd5..c165e16c 100644 --- a/app/controllers/templates_detect_fields_controller.rb +++ b/app/controllers/templates_detect_fields_controller.rb @@ -6,6 +6,26 @@ class TemplatesDetectFieldsController < ApplicationController load_and_authorize_resource :template def create + if params[:algorithm].present? + create_with_algorithm + else + create_with_ml_detection + end + end + + private + + def create_with_algorithm + documents = @template.schema_documents.preload(:blob) + + fields = Templates::FieldDetection.call(@template, params[:algorithm], documents) + + render json: { fields: fields, submitters: @template.submitters, completed: true } + rescue ArgumentError => e + render json: { error: e.message }, status: :unprocessable_content + end + + def create_with_ml_detection response.headers['Content-Type'] = 'text/event-stream' sse = SSE.new(response.stream) diff --git a/app/javascript/application.js b/app/javascript/application.js index 24671b2c..546caf0e 100644 --- a/app/javascript/application.js +++ b/app/javascript/application.js @@ -186,7 +186,8 @@ safeRegisterElement('template-builder', class extends HTMLElement { withDownload: true, currencies: (this.dataset.currencies || '').split(',').filter(Boolean), acceptFileTypes: this.dataset.acceptFileTypes, - showTourStartForm: this.dataset.showTourStartForm === 'true' + showTourStartForm: this.dataset.showTourStartForm === 'true', + detectionAlgorithms: JSON.parse(this.dataset.detectionAlgorithms || '[]') }) this.component = this.app.mount(this.appElem) diff --git a/app/javascript/template_builder/builder.vue b/app/javascript/template_builder/builder.vue index 4830f601..5e76d8aa 100644 --- a/app/javascript/template_builder/builder.vue +++ b/app/javascript/template_builder/builder.vue @@ -509,6 +509,7 @@ :field-types="fieldTypes" :with-sticky-submitters="withStickySubmitters" :with-fields-detection="withFieldsDetection" + :detection-algorithms="detectionAlgorithms" :with-signature-id="withSignatureId" :with-prefillable="withPrefillable" :only-defined-fields="onlyDefinedFields" @@ -738,6 +739,11 @@ export default { required: false, default: false }, + detectionAlgorithms: { + type: Array, + required: false, + default: () => [] + }, withCustomFields: { type: Boolean, required: false, diff --git a/app/javascript/template_builder/fields.vue b/app/javascript/template_builder/fields.vue index 7155414d..1d345fec 100644 --- a/app/javascript/template_builder/fields.vue +++ b/app/javascript/template_builder/fields.vue @@ -325,7 +325,89 @@ v-if="!isShowVariables && withFieldsDetection && editable && fields.length < 2 && !template.schema.some((item) => item.dynamic)" class="my-2" > +
+ + +