Skip to content

Commit

Permalink
feat: integrate the custom text-editor widget for source annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviareichl committed Dec 10, 2024
1 parent 5637580 commit aca02cd
Show file tree
Hide file tree
Showing 11 changed files with 526 additions and 5 deletions.
50 changes: 49 additions & 1 deletion openatlas/forms/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,34 @@ class ValueFloatField(FloatField):
pass


class TextAnnotation(HiddenInput):
def __call__(self, field: Any, **kwargs: Any) -> str:

source_text = field.data
linked_entities = field.linked_entities

return super().__call__(field, **kwargs) + Markup(
render_template('text-annotation.html',
field=field,
source_text=source_text,
linked_entities=linked_entities)
)


class TextAnnotationField(Field):
widget = TextAnnotation()

def __init__(self, label=None, validators=None, source_text=None, linked_entities=None, **kwargs):

super().__init__(label, validators, **kwargs)

self.source_text = source_text or ''
self.linked_entities = linked_entities or []

def _value(self):
return self.data or ''


class TableSelect(HiddenInput):
def __call__(self, field: Any, **kwargs: Any) -> str:

Expand Down Expand Up @@ -253,7 +281,6 @@ class SimpleEntityForm(FlaskForm):
return super().__call__(field, **kwargs) + Markup(
render_template('forms/table_select.html', field=field))


class TableField(HiddenField):
widget = TableSelect()

Expand Down Expand Up @@ -458,6 +485,27 @@ class SubmitField(BooleanField):
widget = SubmitInput()


class SubmitSourceInput(Input):
input_type = 'submit'

def __call__(self, field: Field, **kwargs: Any) -> str:
onclick_event = "saveAnnotationText();"
kwargs['class_'] = (kwargs['class_'] + ' uc-first') \
if 'class_' in kwargs else 'uc-first'
kwargs['onclick'] = onclick_event
return Markup(
f'''
<button
type="submit"
id="{field.id}"
{self.html_params(name=field.name, **kwargs)}
>{field.label.text}</button>''')


class SubmitSourceField(BooleanField):
widget = SubmitSourceInput()


def generate_password_field() -> CustomField:
return CustomField(
'',
Expand Down
1 change: 0 additions & 1 deletion openatlas/forms/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def get_table_form(classes: list[str], excluded: list[int]) -> str:
'forms/form_table.html',
table=table.display(classes[0]))


def get_cidoc_form() -> Any:
class Form(FlaskForm):
pass
Expand Down
25 changes: 23 additions & 2 deletions openatlas/forms/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
ActorBaseManager, ArtifactBaseManager, BaseManager, EventBaseManager,
HierarchyBaseManager, PlaceBaseManager, TypeBaseManager)
from openatlas.forms.field import (
DragNDropField, SubmitField, TableField, TableMultiField, TreeField)
DragNDropField, SubmitField, TableField, TableMultiField, TreeField, TextAnnotationField, SubmitSourceField)
from openatlas.forms.validation import file
from openatlas.models.entity import Entity
from openatlas.models.reference_system import ReferenceSystem
Expand Down Expand Up @@ -576,7 +576,16 @@ class SourceManager(BaseManager):
fields = ['name', 'continue', 'description']

def add_description(self) -> None:
setattr(self.form_class, 'description', TextAreaField(_('content')))

setattr(self.form_class, 'description', TextAnnotationField(
label=_('content'),
source_text="Test Text",
# source_text: html with <mark> attributes that holds the meta attribute with id and comment as annotations
linked_entities=[
{'id': 1, 'name': 'Entity 1'},
{'id': 2, 'name': 'Entity 2'}
]
))

def additional_fields(self) -> dict[str, Any]:
selection = None
Expand All @@ -601,6 +610,18 @@ def process_form(self) -> None:
if self.form.artifact.data:
self.add_link('P128', self.form.artifact.data, inverse=True)

def add_buttons(self) -> None:
setattr(
self.form_class,
'save',
SubmitSourceField(_('insert') if self.insert else _('save')))
if self.insert and 'continue' in self.fields:
setattr(
self.form_class,
'insert_and_continue',
SubmitSourceField(_('insert and continue')))
setattr(self.form_class, 'continue_', HiddenField())


class SourceTranslationManager(BaseManager):
fields = ['name', 'continue']
Expand Down
2 changes: 1 addition & 1 deletion openatlas/forms/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def hierarchy_name_exists(form: Any, field: Any) -> None:


def validate(form: FlaskForm, extra_validators: validators = None) -> bool:
valid = FlaskForm.validate(form, extra_validators)
valid = FlaskForm.validate(form)
if hasattr(form, 'begin_year_from') and not validate_dates(form):
valid = False
for field_id, field in form.__dict__.items():
Expand Down
Loading

0 comments on commit aca02cd

Please sign in to comment.