diff --git a/build.gradle.kts b/build.gradle.kts index 535e72de..3eb48ef6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -221,13 +221,4 @@ spotless { ), ) } - kotlinGradle { - ktlint("1.3.0") - .editorConfigOverride( - mapOf( - "max_line_length" to "256", - "insert_final_newline" to "true", - ), - ) - } } diff --git a/src/commonMain/kotlin/com.github.ikovalyov.model/Comment.kt b/src/commonMain/kotlin/com.github.ikovalyov.model/Comment.kt new file mode 100644 index 00000000..a200608d --- /dev/null +++ b/src/commonMain/kotlin/com.github.ikovalyov.model/Comment.kt @@ -0,0 +1,97 @@ +@file:UseSerializers(UuidSerializer::class) + +package com.github.ikovalyov.model + +import com.benasher44.uuid.Uuid +import com.benasher44.uuid.uuidFrom +import com.github.ikovalyov.model.markers.IEditable +import com.github.ikovalyov.model.security.User +import com.github.ikovalyov.model.serializer.UuidSerializer +import kotlinx.serialization.Serializable +import kotlinx.serialization.UseSerializers +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json + +@Serializable +data class Comment(override val id: Uuid, val body: String, val author: User, val article: Article, val userList: List, val articleList: List
) : IEditable { + override fun getMetadata(): List> = listOf( + IEditable.EditableMetadata( + fieldType = IEditable.FieldType.Id, + readOnly = true, + serialize = { + it.toString() + }, + deserialize = { + uuidFrom(it) + }, + update = { + copy(id = it) + }, + get = { + id + }, + fieldName = "Id", + predefinedList = null, + ), + IEditable.EditableMetadata( + fieldType = IEditable.FieldType.Body, + readOnly = true, + serialize = { + it + }, + deserialize = { + it + }, + update = { + copy(body = it) + }, + get = { + body + }, + fieldName = "Body", + predefinedList = null, + ), + IEditable.EditableMetadata( + fieldType = IEditable.FieldType.Author, + readOnly = false, + serialize = { + it.id.toString() + }, + deserialize = { uuid -> + userList.first { + it.id.toString() == uuid + } + }, + update = { + copy(author = it) + }, + get = { + author + }, + fieldName = "Author", + predefinedList = userList, + ), + IEditable.EditableMetadata( + fieldType = IEditable.FieldType.Article, + readOnly = false, + serialize = { + it.id.toString() + }, + deserialize = { uuid -> + articleList.first { + it.id.toString() == uuid + } + }, + update = { + copy(article = it) + }, + get = { + article + }, + fieldName = "Author", + predefinedList = articleList, + ), + ) + + override fun serialize(): String = Json.encodeToString(this) +} diff --git a/src/commonMain/kotlin/com.github.ikovalyov.model/markers/IEditable.kt b/src/commonMain/kotlin/com.github.ikovalyov.model/markers/IEditable.kt index 32a78f3b..a2f941fc 100644 --- a/src/commonMain/kotlin/com.github.ikovalyov.model/markers/IEditable.kt +++ b/src/commonMain/kotlin/com.github.ikovalyov.model/markers/IEditable.kt @@ -3,7 +3,6 @@ package com.github.ikovalyov.model.markers import com.benasher44.uuid.Uuid import com.github.ikovalyov.model.security.User import kotlinx.datetime.Instant -import kotlinx.serialization.ExperimentalSerializationApi interface IEditable { data class EditableMetadata( @@ -19,8 +18,6 @@ interface IEditable { sealed class FieldType { object Id : FieldType() - - @OptIn(ExperimentalSerializationApi::class) object Author : FieldType() object LastModified : FieldType() object Body : FieldType() @@ -33,9 +30,8 @@ interface IEditable { object StringListFiledType : FieldType>() object Tags : FieldType>() object Metadata : FieldType>() - - @OptIn(ExperimentalSerializationApi::class) object Template : FieldType() + object Article : FieldType() } val id: Uuid @@ -48,14 +44,14 @@ fun T.updateField(field: IEditable.EditableMetadata T.getFieldValueAsString(field: IEditable.EditableMetadata): String? { +fun getFieldValueAsString(field: IEditable.EditableMetadata): String? { val fieldValue = field.get() return fieldValue?.let { field.serialize(it) } } -fun T.getPredefinedValuesAsStrings(field: IEditable.EditableMetadata): Map { +fun getPredefinedValuesAsStrings(field: IEditable.EditableMetadata): Map { val fieldValues = field.predefinedList return fieldValues?.associate { it.hashCode().toString() to field.serialize(it) diff --git a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateEdit.kt b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateEdit.kt index 3c0b0658..340fb1e0 100644 --- a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateEdit.kt +++ b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateEdit.kt @@ -88,7 +88,7 @@ class TemplateEdit(props: ItemEditProps, private val initialSt name = it.hashCode().toString() readOnly = it.readOnly if (!it.readOnly) { - defaultValue = state.item.getFieldValueAsString(it) + defaultValue = getFieldValueAsString(it) onChange = { event -> launch { @@ -101,7 +101,7 @@ class TemplateEdit(props: ItemEditProps, private val initialSt } } } else { - value = state.item.getFieldValueAsString(it) + value = getFieldValueAsString(it) } } } else { @@ -111,7 +111,7 @@ class TemplateEdit(props: ItemEditProps, private val initialSt if (!it.readOnly) { val storedObject = it.get() if (storedObject != null) { - defaultValue = state.item.getFieldValueAsString(it) + defaultValue = getFieldValueAsString(it) } onChange = { event -> @@ -119,7 +119,7 @@ class TemplateEdit(props: ItemEditProps, private val initialSt state.item = state.item.updateField(field = it, serializedData = stringValue) } - val optionsList = state.item.getPredefinedValuesAsStrings(it) + val optionsList = getPredefinedValuesAsStrings(it) optionsList.forEach { option { value = it.key diff --git a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateInsert.kt b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateInsert.kt index 62aa0a3c..953e1196 100644 --- a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateInsert.kt +++ b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateInsert.kt @@ -96,7 +96,7 @@ class TemplateInsert(props: TemplateInsertProps, initialState: } } } else { - value = state.currentItem.getFieldValueAsString(it) + value = getFieldValueAsString(it) } } } else { @@ -106,7 +106,7 @@ class TemplateInsert(props: TemplateInsertProps, initialState: if (!it.readOnly) { val storedObject = it.get() if (storedObject != null) { - defaultValue = state.currentItem.getFieldValueAsString(it) + defaultValue = getFieldValueAsString(it) } onChange = { event -> @@ -119,7 +119,7 @@ class TemplateInsert(props: TemplateInsertProps, initialState: ) } } - val optionsList = state.currentItem.getPredefinedValuesAsStrings(it) + val optionsList = getPredefinedValuesAsStrings(it) optionsList.forEach { ReactHTML.option { value = it.key diff --git a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateView.kt b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateView.kt index 7a65725c..2d5651b4 100644 --- a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateView.kt +++ b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/TemplateView.kt @@ -44,7 +44,7 @@ class TemplateView(props: TemplateViewProps, state: TemplateVi fields.forEach { section { h1 { +it.fieldType::class.simpleName!! } - p { +(state.item.getFieldValueAsString(it) ?: "") } + p { +(getFieldValueAsString(it) ?: "") } } } buttonChild { diff --git a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/table/Table.kt b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/table/Table.kt index d6bc9189..091f4cff 100644 --- a/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/table/Table.kt +++ b/src/jsMain/kotlin/com.github.ikovalyov/react/components/template/table/Table.kt @@ -67,7 +67,7 @@ private fun buildTableColumns(componentProps: TableProps, ite accessorFn = { row, _ -> val itemMetadata = row.getMetadata().filterIsInstance>()[counter] - val str = row.getFieldValueAsString(itemMetadata) ?: "" + val str = getFieldValueAsString(itemMetadata) ?: "" if (str.length > 128) { str.substring(0, 128) } else {