OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
make blocknote input part of the form DSL
Bruno Pagno10e839fb4172
diff --git a/frontend/src/react/OpBlockNoteContainer.tsx b/frontend/src/react/OpBlockNoteContainer.tsxindex 7938fe36107..84a133c7abf 100644--- a/frontend/src/react/OpBlockNoteContainer.tsx+++ b/frontend/src/react/OpBlockNoteContainer.tsx@@ -35,7 +35,7 @@ import { dummyBlockSpec, getDefaultOpenProjectSlashMenuItems, openProjectWorkPacimport { useEffect, useState } from "react";interface OpBlockNoteContainerProps {- inputName?: string;+ inputField: HTMLInputElement;inputText?: string;}@@ -47,9 +47,8 @@ const schema = BlockNoteSchema.create({},});-export default function OpBlockNoteContainer({ inputName, inputText }: OpBlockNoteContainerProps) {+export default function OpBlockNoteContainer({ inputField, inputText }: OpBlockNoteContainerProps) {const [isLoading, setIsLoading] = useState(true);- const [editorContent, setEditorContent] = useState(inputText || "");const editor = useCreateBlockNote({ schema });type EditorType = typeof editor;@@ -65,7 +64,6 @@ export default function OpBlockNoteContainer({ inputName, inputText }: OpBlockNoasync function loadInitialContent() {const blocks = await editor.tryParseMarkdownToBlocks(inputText || "");editor.replaceBlocks(editor.document, blocks);- setEditorContent(await editor.blocksToMarkdownLossy());setIsLoading(false);}loadInitialContent();@@ -73,14 +71,13 @@ export default function OpBlockNoteContainer({ inputName, inputText }: OpBlockNoreturn (<>- <input type="hidden" name={inputName} value={editorContent} />{isLoading ? <div>Loading...</div>:<BlockNoteVieweditor={editor}onChange={async (editor) => {const content = await editor.blocksToMarkdownLossy();- setEditorContent(content);+ inputField.value = content;}}><SuggestionMenuControllerdiff --git a/frontend/src/stimulus/controllers/dynamic/block-note.controller.ts b/frontend/src/stimulus/controllers/dynamic/block-note.controller.tsindex d60855f77f8..4ecc7d25a80 100644--- a/frontend/src/stimulus/controllers/dynamic/block-note.controller.ts+++ b/frontend/src/stimulus/controllers/dynamic/block-note.controller.ts@@ -34,15 +34,17 @@ import { createRoot } from 'react-dom/client';import OpBlockNoteContainer from 'react/OpBlockNoteContainer';export default class extends Controller {- static targets = ['blockNoteEditor'];+ static targets = [+ 'blockNoteEditor',+ 'blockNoteInputField',+ ];static values = {- inputName: String,inputText: String,};- declare readonly blockNoteEditorTarget:HTMLElement;- declare readonly inputNameValue:string;- declare readonly inputTextValue:string;+ declare readonly blockNoteEditorTarget: HTMLElement;+ declare readonly blockNoteInputFieldTarget: HTMLInputElement;+ declare readonly inputTextValue: string;connect() {const root = createRoot(this.blockNoteEditorTarget);@@ -51,7 +53,7 @@ export default class extends Controller {BlockNoteReactContainer() {return React.createElement(OpBlockNoteContainer, {- inputName: this.inputNameValue,+ inputField: this.blockNoteInputFieldTarget,inputText: this.inputTextValue,});}diff --git a/app/components/block_note_editor.html.erb b/lib/primer/open_project/forms/block_note_editor.html.erbsimilarity index 71%rename from app/components/block_note_editor.html.erbrename to lib/primer/open_project/forms/block_note_editor.html.erbindex 3eddd73a260..a56e7a1e458 100644--- a/app/components/block_note_editor.html.erb+++ b/lib/primer/open_project/forms/block_note_editor.html.erb@@ -28,16 +28,26 @@ See COPYRIGHT and LICENSE files for more details.++#%><%=- render(- Primer::BaseComponent.new(- tag: :div,- border: true, border_radius: 2, mt: 1, mb: 1,- data: {- controller: "block-note",- block_note_target: "blockNoteEditor",- block_note_input_name_value: input_name,- block_note_input_text_value: value- }- )- )+ render FormControl.new(+ input: @input, data: {+ controller: "block-note",+ block_note_input_text_value: value+ }+ ) do%>+ <%=+ @input.builder.hidden_field(+ name, value: @value,+ data: { block_note_target: "blockNoteInputField" }+ )+ %>+ <%=+ render(+ Primer::BaseComponent.new(+ tag: :div,+ border: true, border_radius: 2, mt: 1, mb: 1,+ data: { block_note_target: "blockNoteEditor" }+ )+ )+ %>+<% end %>diff --git a/app/components/block_note_editor.rb b/lib/primer/open_project/forms/block_note_editor.rbsimilarity index 78%rename from app/components/block_note_editor.rbrename to lib/primer/open_project/forms/block_note_editor.rbindex e786a9d87f5..e34aa516dbd 100644--- a/app/components/block_note_editor.rb+++ b/lib/primer/open_project/forms/block_note_editor.rb@@ -28,12 +28,21 @@# See COPYRIGHT and LICENSE files for more details.#++-class BlockNoteEditor < ApplicationComponent- attr_reader :input_name, :value+module Primer+ module OpenProject+ module Forms+ # :nodoc:+ class BlockNoteEditor < Primer::Forms::BaseComponent+ attr_reader :input, :value- def initialize(input_name, value, **)- super(**)- @input_name = input_name- @value = value+ delegate :name, to: :@input++ def initialize(input:, value:)+ super()+ @input = input+ @value = value+ end+ end+ endendenddiff --git a/lib/primer/open_project/forms/dsl/block_note_editor_input.rb b/lib/primer/open_project/forms/dsl/block_note_editor_input.rbnew file mode 100644index 00000000000..56c7997de55--- /dev/null+++ b/lib/primer/open_project/forms/dsl/block_note_editor_input.rb@@ -0,0 +1,61 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module Primer+ module OpenProject+ module Forms+ module Dsl+ class BlockNoteEditorInput < Primer::Forms::Dsl::Input+ attr_reader :name, :label, :value++ def initialize(name:, label:, value:, **system_arguments)+ @name = name+ @label = label+ @value = value++ super(**system_arguments)+ end++ def to_component+ BlockNoteEditor.new(input: self, value:)+ end++ def type+ :block_note_editor+ end++ def focusable?+ true+ end+ end+ end+ end+ end+enddiff --git a/lib/primer/open_project/forms/dsl/input_methods.rb b/lib/primer/open_project/forms/dsl/input_methods.rbindex 5ff89481631..a0c314d1a9e 100644--- a/lib/primer/open_project/forms/dsl/input_methods.rb+++ b/lib/primer/open_project/forms/dsl/input_methods.rb@@ -25,8 +25,8 @@ module Primeradd_input AutocompleterInput.new(builder:, form:, **decorate_options(**), &)end- def pattern_input(**, &)- add_input PatternInput.new(builder:, form:, **decorate_options(**), &)+ def block_note_editor(**, &)+ add_input BlockNoteEditorInput.new(builder:, form:, **decorate_options(**), &)enddef color_select_list(**, &)@@ -37,6 +37,10 @@ module Primeradd_input HtmlContentInput.new(builder:, form:, **, &)end+ def pattern_input(**, &)+ add_input PatternInput.new(builder:, form:, **decorate_options(**), &)+ end+def project_autocompleter(**, &)add_input ProjectAutocompleterInput.new(builder:, form:, **decorate_options(**), &)enddiff --git a/modules/documents/app/forms/document_form.rb b/modules/documents/app/forms/document_form.rbnew file mode 100644index 00000000000..f436289acbf--- /dev/null+++ b/modules/documents/app/forms/document_form.rb@@ -0,0 +1,92 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++class DocumentForm < ApplicationForm+ form do |f|+ f.select_list(+ name: :category_id,+ label: I18n.t("label_document_category"),+ required: true+ ) do |select|+ DocumentCategory.find_each do |category|+ select.option(value: category.id, label: category.name)+ end+ end++ f.text_field(+ name: :title,+ label: I18n.t("label_document_title"),+ required: true+ )++ if OpenProject::FeatureDecisions.block_note_editor_active? && model.category&.name == "Experimental"+ f.block_note_editor(+ name: :description,+ label: I18n.t("label_document_description"),+ value: model.description+ )+ else+ f.rich_text_area(+ name: :description,+ label: I18n.t("label_document_description"),+ rich_text_options: {+ with_text_formatting: true,+ resource:,+ turboMode: false+ }+ )+ end++ f.submit(+ name: :save,+ label: save_label,+ class: "-with-icon icon-checkmark",+ scheme: :primary+ )+ end++ private++ def resource+ return unless model++ API::V3::Documents::DocumentRepresenter.create(+ model, current_user: User.current, embed_links: true+ )+ end++ def save_label+ if model.persisted?+ I18n.t("button_save")+ else+ I18n.t("button_create")+ end+ end+enddiff --git a/modules/documents/app/views/documents/_form.html.erb b/modules/documents/app/views/documents/_form.html.erbdeleted file mode 100644index b2d9598681b..00000000000--- a/modules/documents/app/views/documents/_form.html.erb+++ /dev/null@@ -1,54 +0,0 @@-<%#-- copyright-OpenProject is an open source project management software.-Copyright (C) the OpenProject GmbH--This program is free software; you can redistribute it and/or-modify it under the terms of the GNU General Public License version 3.--OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:-Copyright (C) 2006-2013 Jean-Philippe Lang-Copyright (C) 2010-2013 the ChiliProject Team--This program is free software; you can redistribute it and/or-modify it under the terms of the GNU General Public License-as published by the Free Software Foundation; either version 2-of the License, or (at your option) any later version.--This program is distributed in the hope that it will be useful,-but WITHOUT ANY WARRANTY; without even the implied warranty of-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the-GNU General Public License for more details.--You should have received a copy of the GNU General Public License-along with this program; if not, write to the Free Software-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.--See COPYRIGHT and LICENSE files for more details.--++#%>--<%= error_messages_for @document %>--<div class="form--field -required">- <%= f.select :category_id,- DocumentCategory.all.map { |c| [c.name, c.id] },- required: true,- container_class: "-slim" %>-</div>-<div class="form--field -required">- <%= f.text_field :title, required: true, container_class: "-wide" %>-</div>-<% if OpenProject::FeatureDecisions.block_note_editor_active? %>- <%= render BlockNoteEditor.new("#{f.object_name}[description]", f.object.description) %>-<% else %>- <div class="form--field -visible-overflow">- <%= f.text_area :description,- container_class: "-xxwide",- label_options: {- class: "-top"- },- with_text_formatting: true,- resource: api_v3_document_resource(f.object),- preview_context: preview_context(f.object.project) %>- </div>-<% end %>diff --git a/modules/documents/app/views/documents/edit.html.erb b/modules/documents/app/views/documents/edit.html.erbindex 85a1f2ca7d8..778848cccc9 100644--- a/modules/documents/app/views/documents/edit.html.erb+++ b/modules/documents/app/views/documents/edit.html.erb@@ -38,7 +38,12 @@ See COPYRIGHT and LICENSE files for more details.end%>-<%= labelled_tabular_form_for @document, url: document_path(@document), method: "PATCH" do |f| -%>- <%= render partial: "documents/form", locals: { f: f } %>- <%= styled_button_tag t(:button_save), class: "-primary -with-icon icon-checkmark" %>-<% end %>+<%=+ primer_form_with(+ model: @document,+ url: document_path(@document),+ method: :patch+ ) do |f|+ render DocumentForm.new(f)+ end+%>diff --git a/modules/documents/app/views/documents/new.html.erb b/modules/documents/app/views/documents/new.html.erbindex dfd361b64d3..af89322c2e8 100644--- a/modules/documents/app/views/documents/new.html.erb+++ b/modules/documents/app/views/documents/new.html.erb@@ -38,7 +38,8 @@ See COPYRIGHT and LICENSE files for more details.end%>-<%= labelled_tabular_form_for @document, url: project_documents_path(@project), html: { multipart: true } do |f| -%>- <%= render partial: "documents/form", locals: { f: f } %>- <%= styled_button_tag t(:button_create), class: "-primary -with-icon icon-checkmark" %>-<% end %>+<%=+ primer_form_with(model: @document, url: project_documents_path(@project), data: { test_selector: "new-document" }) do |f|+ render DocumentForm.new(f)+ end+%>diff --git a/modules/documents/config/locales/en.yml b/modules/documents/config/locales/en.ymlindex f51cac5d8e8..38b4d188993 100644--- a/modules/documents/config/locales/en.yml+++ b/modules/documents/config/locales/en.yml@@ -53,6 +53,9 @@ en:label_document_new: "New document"label_document_plural: "Documents"label_documents: "Documents"+ label_document_title: "Title"+ label_document_description: "Description"+ label_document_category: "Category"permission_manage_documents: "Manage documents"permission_view_documents: "View documents"diff --git a/modules/documents/spec/controllers/documents_controller_spec.rb b/modules/documents/spec/controllers/documents_controller_spec.rbindex 96e75c022db..1c0319d37f0 100644--- a/modules/documents/spec/controllers/documents_controller_spec.rb+++ b/modules/documents/spec/controllers/documents_controller_spec.rb@@ -67,8 +67,14 @@ RSpec.describe DocumentsController doget :new, params: { project_id: project.id }end+ it "returns a successful response" do+ expect(response).to be_successful+ end+it "show the new document form" do- expect(response).to render_template(partial: "documents/_form")+ expect(response.body).to include("Category")+ expect(response.body).to include("Title")+ expect(response.body).to include("Description")endenddiff --git a/modules/documents/spec/features/attachment_upload_spec.rb b/modules/documents/spec/features/attachment_upload_spec.rbindex 6d82d227719..7b3525f9424 100644--- a/modules/documents/spec/features/attachment_upload_spec.rb+++ b/modules/documents/spec/features/attachment_upload_spec.rb@@ -61,7 +61,7 @@ RSpec.describe "Upload attachment to documents",it "can upload an image" dovisit new_project_document_path(project)- expect(page).to have_css("#new_document", wait: 10)+ expect(page).to have_css('[data-test-selector="new-document"]', wait: 10)SeleniumHubWaiter.waitselect(category.name, from: "Category")fill_in "Title", with: "New documentation"