Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F12656470
edit-jdd-field.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
edit-jdd-field.ts
View Options
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/consistent-type-assertions */
/* eslint-disable @typescript-eslint/no-unused-vars */
import
type
Router
from
"@koa/router"
;
import
type
{
JDDocumentContainer
,
RawJDDocument
}
from
"@sealcode/jdd"
;
import
{
documentContainerFromStorage
,
documentToParsed
,
documentToStorage
,
}
from
"@sealcode/jdd"
;
import
type
{
Context
}
from
"koa"
;
import
type
{
Collection
,
CollectionItem
,
FieldNames
}
from
"sealious"
;
import
JDDCreator
from
"./jdd-creator.js"
;
import
type
{
JDDPageState
}
from
"./jdd-page.js"
;
import
{
tempstream
}
from
"tempstream"
;
export
abstract
class
EditJDDField
<
C
extends
Collection
>
extends
JDDCreator
{
async
getID
(
ctx
:
Context
)
:
Promise
<
string
>
{
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-unsafe-member-access
const
id
=
ctx
.
params
[
"id"
]
as
string
;
if
(
!
id
)
{
throw
new
Error
(
"Missing URL parameter: "
+
"id"
);
}
return
id
;
}
abstract
getCollection
(
ctx
:
Context
)
:
C
;
async
getItem
(
ctx
:
Context
)
:
Promise
<
CollectionItem
<
C
>>
{
const
{
items
:
[
item
],
}
=
await
this
.
getCollection
(
ctx
)
.
list
(
ctx
.
$context
)
.
ids
([
await
this
.
getID
(
ctx
)])
.
fetch
();
if
(
!
item
)
{
throw
new
Error
(
"Couldn't get item of id "
+
(
await
this
.
getID
(
ctx
))
);
}
return
item
;
}
abstract
getJDDFieldName
()
:
FieldNames
<
C
[
"fields"
]
>
;
mount
(
router
:
Router
,
path
:
string
)
{
super
.
mount
(
router
,
path
);
router
.
post
(
path
+
"save/"
,
async
(
ctx
)
=>
{
const
{
state
}
=
await
this
.
extractState
(
ctx
);
const
item
=
await
this
.
getItem
(
ctx
);
item
.
set
(
this
.
getJDDFieldName
(),
(
await
documentToStorage
(
this
.
registry
,
this
.
makeJDDContext
(
ctx
),
{
value
:
state
.
components
,
}
as
unknown
as
JDDocumentContainer
<
"parsed"
>
)
).
value
as
any
);
await
item
.
save
(
ctx
.
$context
);
ctx
.
type
=
"html"
;
ctx
.
status
=
422
;
if
(
!
state
.
messages
)
{
state
.
messages
=
[];
}
state
.
messages
.
push
(
"Saved!"
);
ctx
.
body
=
this
.
render
(
ctx
,
state
);
});
}
async
renderHeader
(
_ctx
:
Context
,
_item
:
CollectionItem
<
C
>
)
{
return
/* HTML */
`<h1>Edit JDD</h1>`
;
}
async
renderPreParameterButtons
(
ctx
:
Context
)
{
const
item
=
await
this
.
getItem
(
ctx
);
return
tempstream
`<div>
${
this
.
renderHeader
(
ctx
,
item
)
}
</div>`
;
}
renderParameterButtons
(
state
:
JDDPageState
)
{
{
/*The below button has to be here in order for it to be the default behavior */
}
return
`<div class="jdd-editor__toolbar">
<input type="submit" value="Preview" />
<select name="component">
${
Object
.
keys
(
this
.
getRegistryComponents
())
.
map
((
cmp
)
=>
`<option value="
${
cmp
}
">
${
cmp
}
</option>`
)
.
join
(
""
)
}
</select>
${
this
.
makeActionButton
(
state
,
{
action
:
"add_component"
,
label
:
"Add component"
,
}
)}
<input type="submit" formaction="./save/" value="zapisz" />
</div>`
;
}
async
getInitialState
(
ctx
:
Context
)
{
const
article
=
await
this
.
getItem
(
ctx
);
const
parsed_document
=
await
documentToParsed
(
this
.
registry
,
this
.
makeJDDContext
(
ctx
),
documentContainerFromStorage
(
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
(
article
.
get
(
this
.
getJDDFieldName
())
as
RawJDDocument
)
||
[]
)
);
return
{
components
:
parsed_document
.
value
.
map
((
e
)
=>
({
...
e
,
open
:
true
,
})),
};
}
// uncomment to create whitelist of allowed components
// getAllowedComponents() {
// return ["nice-box"];
// }
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Nov 28, 15:11 (1 d, 6 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1059747
Default Alt Text
edit-jdd-field.ts (3 KB)
Attached To
Mode
rJDDE jdd-editor
Attached
Detach File
Event Timeline
Log In to Comment