@@ -144,3 +144,113 @@ To get completion suggestions for the text in the editor, you can use the `compl
144144```
145145
146146![ alt text] ( gptDemo.gif )
147+
148+ ### Imges in Rich editor
149+
150+ First, you need to create resource for images:
151+ ``` prisma title="schema.prisma"
152+ model description_image {
153+ id String @id
154+ created_at DateTime
155+ resource_id String
156+ record_id String
157+ image_path String
158+ }
159+ ```
160+
161+ ``` bash
162+ npm run makemigration -- --name add_description_image
163+ ```
164+
165+ ``` bash
166+ npm i @adminforth/upload --save
167+ npm i @adminforth/storage-adapter-local --save
168+ ```
169+
170+ ``` typescript title="./resources/description_image.ts"
171+ import AdminForthStorageAdapterLocalFilesystem from " ../../adapters/adminforth-storage-adapter-local" ;
172+ import { AdminForthResourceInput } from " ../../adminforth" ;
173+ import UploadPlugin from " ../../plugins/adminforth-upload" ;
174+ import { v1 as uuid } from " uuid" ;
175+
176+ export default {
177+ dataSource: " maindb" ,
178+ table: " description_image" ,
179+ resourceId: " description_images" ,
180+ label: " Description images" ,
181+ columns: [
182+ {
183+ name: " id" ,
184+ primaryKey: true ,
185+ required: false ,
186+ fillOnCreate : ({ initialRecord }: any ) => uuid (),
187+ showIn: {
188+ create: false ,
189+ },
190+ },
191+ {
192+ name: " created_at" ,
193+ required: false ,
194+ fillOnCreate : ({ initialRecord }: any ) => new Date ().toISOString (),
195+ showIn: {
196+ create: false ,
197+ },
198+ },
199+ { name: " resource_id" , required: false },
200+ { name: " record_id" , required: false },
201+ { name: " image_path" , required: false },
202+ ],
203+ plugins: [
204+ new UploadPlugin ({
205+ pathColumnName: " image_path" ,
206+
207+ // rich editor plugin supports only 'public-read' ACL images for SEO purposes (instead of presigned URLs which change every time)
208+ storageAdapter: new AdminForthStorageAdapterLocalFilesystem ({
209+ fileSystemFolder: " ./db/uploads/description_images" , // folder where files will be stored on disk
210+ adminServeBaseUrl: " static/source" , // the adapter not only stores files, but also serves them for HTTP requests
211+ mode: " public" , // public if all files should be accessible from the web, private only if could be accesed by temporary presigned links
212+ signingSecret: process .env .ADMINFORTH_SECRET , // secret used to generate presigned URLs
213+ }),
214+
215+ allowedFileExtensions: [
216+ " jpg" ,
217+ " jpeg" ,
218+ " png" ,
219+ " gif" ,
220+ " webm" ,
221+ " exe" ,
222+ " webp" ,
223+ ],
224+ maxFileSize: 1024 * 1024 * 20 , // 5MB
225+
226+
227+ filePath : ({ originalFilename , originalExtension , contentType }) =>
228+ ` description_images/${new Date ().getFullYear ()}/${uuid ()}/${originalFilename }.${originalExtension } ` ,
229+
230+ preview: {
231+ // Used to display preview (if it is image) in list and show views instead of just path
232+ // previewUrl: ({s3Path}) => `https://tmpbucket-adminforth.s3.eu-central-1.amazonaws.com/${s3Path}`,
233+ // show image preview instead of path in list view
234+ // showInList: false,
235+ },
236+ }),
237+ ],
238+ } as AdminForthResourceInput ;
239+ ```
240+
241+ Next, add attachments to RichEditor plugin:
242+
243+ ``` typescript title="./resources/apartments.ts"
244+ import RichEditorPlugin from ' @adminforth/rich-editor' ;
245+
246+ // ... existing resource configuration ...
247+
248+ new RichEditorPlugin ({
249+ htmlFieldName: ' description' ,
250+ attachments: {
251+ attachmentResource: " description_images" ,
252+ attachmentFieldName: " image_path" ,
253+ attachmentRecordIdFieldName: " record_id" ,
254+ attachmentResourceIdFieldName: " resource_id" ,
255+ }
256+ })
0 commit comments