-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sandbox: add "custom links" to editor
- Loading branch information
Showing
9 changed files
with
441 additions
and
85 deletions.
There are no files selected for viewing
273 changes: 273 additions & 0 deletions
273
packages/admin-sandbox/api/migrations/2022-01-17-164045-links.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,273 @@ | ||
{ | ||
"formatVersion": 3, | ||
"modifications": [ | ||
{ | ||
"modification": "removeField", | ||
"entityName": "ContentReference", | ||
"fieldName": "url" | ||
}, | ||
{ | ||
"modification": "createEnum", | ||
"enumName": "LinkType", | ||
"values": [ | ||
"internal", | ||
"external" | ||
] | ||
}, | ||
{ | ||
"modification": "createEnum", | ||
"enumName": "UrlType", | ||
"values": [ | ||
"article", | ||
"category", | ||
"tag" | ||
] | ||
}, | ||
{ | ||
"modification": "createEntity", | ||
"entity": { | ||
"name": "Link", | ||
"primary": "id", | ||
"primaryColumn": "id", | ||
"unique": {}, | ||
"fields": { | ||
"id": { | ||
"name": "id", | ||
"columnName": "id", | ||
"nullable": false, | ||
"type": "Uuid", | ||
"columnType": "uuid" | ||
} | ||
}, | ||
"tableName": "link" | ||
} | ||
}, | ||
{ | ||
"modification": "createEntity", | ||
"entity": { | ||
"name": "Url", | ||
"primary": "id", | ||
"primaryColumn": "id", | ||
"unique": {}, | ||
"fields": { | ||
"id": { | ||
"name": "id", | ||
"columnName": "id", | ||
"nullable": false, | ||
"type": "Uuid", | ||
"columnType": "uuid" | ||
} | ||
}, | ||
"tableName": "url" | ||
} | ||
}, | ||
{ | ||
"modification": "createColumn", | ||
"entityName": "Link", | ||
"field": { | ||
"name": "type", | ||
"columnName": "type", | ||
"nullable": false, | ||
"type": "Enum", | ||
"columnType": "LinkType" | ||
} | ||
}, | ||
{ | ||
"modification": "createColumn", | ||
"entityName": "Link", | ||
"field": { | ||
"name": "externalLink", | ||
"columnName": "external_link", | ||
"nullable": true, | ||
"type": "String", | ||
"columnType": "text" | ||
} | ||
}, | ||
{ | ||
"modification": "createColumn", | ||
"entityName": "Url", | ||
"field": { | ||
"name": "url", | ||
"columnName": "url", | ||
"nullable": false, | ||
"type": "String", | ||
"columnType": "text" | ||
} | ||
}, | ||
{ | ||
"modification": "createColumn", | ||
"entityName": "Url", | ||
"field": { | ||
"name": "type", | ||
"columnName": "type", | ||
"nullable": false, | ||
"type": "Enum", | ||
"columnType": "UrlType" | ||
} | ||
}, | ||
{ | ||
"modification": "createRelation", | ||
"entityName": "ContentReference", | ||
"owningSide": { | ||
"name": "link", | ||
"nullable": true, | ||
"type": "ManyHasOne", | ||
"target": "Link", | ||
"joiningColumn": { | ||
"columnName": "link_id", | ||
"onDelete": "restrict" | ||
} | ||
} | ||
}, | ||
{ | ||
"modification": "createRelation", | ||
"entityName": "Link", | ||
"owningSide": { | ||
"name": "internalLink", | ||
"nullable": true, | ||
"type": "ManyHasOne", | ||
"target": "Url", | ||
"joiningColumn": { | ||
"columnName": "internal_link_id", | ||
"onDelete": "set null" | ||
} | ||
} | ||
}, | ||
{ | ||
"modification": "createRelation", | ||
"entityName": "Url", | ||
"owningSide": { | ||
"name": "article", | ||
"nullable": true, | ||
"type": "OneHasOne", | ||
"target": "Article", | ||
"joiningColumn": { | ||
"columnName": "article_id", | ||
"onDelete": "cascade" | ||
} | ||
} | ||
}, | ||
{ | ||
"modification": "createRelation", | ||
"entityName": "Url", | ||
"owningSide": { | ||
"name": "category", | ||
"nullable": true, | ||
"type": "OneHasOne", | ||
"target": "Category", | ||
"joiningColumn": { | ||
"columnName": "category_id", | ||
"onDelete": "cascade" | ||
} | ||
} | ||
}, | ||
{ | ||
"modification": "createRelation", | ||
"entityName": "Url", | ||
"owningSide": { | ||
"name": "tag", | ||
"nullable": true, | ||
"type": "OneHasOne", | ||
"target": "Tag", | ||
"joiningColumn": { | ||
"columnName": "tag_id", | ||
"onDelete": "cascade" | ||
} | ||
} | ||
}, | ||
{ | ||
"modification": "createUniqueConstraint", | ||
"entityName": "Url", | ||
"unique": { | ||
"fields": [ | ||
"url" | ||
], | ||
"name": "unique_Url_url_a758c1" | ||
} | ||
}, | ||
{ | ||
"modification": "patchAclSchema", | ||
"patch": [ | ||
{ | ||
"op": "add", | ||
"path": "/roles/admin/entities/Link", | ||
"value": { | ||
"predicates": {}, | ||
"operations": { | ||
"create": { | ||
"id": true, | ||
"type": true, | ||
"internalLink": true, | ||
"externalLink": true | ||
}, | ||
"read": { | ||
"id": true, | ||
"type": true, | ||
"internalLink": true, | ||
"externalLink": true | ||
}, | ||
"update": { | ||
"id": true, | ||
"type": true, | ||
"internalLink": true, | ||
"externalLink": true | ||
}, | ||
"delete": true, | ||
"customPrimary": true | ||
} | ||
} | ||
}, | ||
{ | ||
"op": "add", | ||
"path": "/roles/admin/entities/Url", | ||
"value": { | ||
"predicates": {}, | ||
"operations": { | ||
"create": { | ||
"id": true, | ||
"url": true, | ||
"type": true, | ||
"article": true, | ||
"category": true, | ||
"tag": true | ||
}, | ||
"read": { | ||
"id": true, | ||
"url": true, | ||
"type": true, | ||
"article": true, | ||
"category": true, | ||
"tag": true | ||
}, | ||
"update": { | ||
"id": true, | ||
"url": true, | ||
"type": true, | ||
"article": true, | ||
"category": true, | ||
"tag": true | ||
}, | ||
"delete": true, | ||
"customPrimary": true | ||
} | ||
} | ||
}, | ||
{ | ||
"op": "add", | ||
"path": "/roles/admin/entities/ContentReference/operations/create/link", | ||
"value": true | ||
}, | ||
{ | ||
"op": "add", | ||
"path": "/roles/admin/entities/ContentReference/operations/update/link", | ||
"value": true | ||
}, | ||
{ | ||
"op": "add", | ||
"path": "/roles/admin/entities/ContentReference/operations/read/link", | ||
"value": true | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { SchemaDefinition as d } from '@contember/schema-definition' | ||
import { Url } from './Url' | ||
|
||
export const LinkType = d.createEnum('internal', 'external') | ||
|
||
export class Link { | ||
type = d.enumColumn(LinkType).notNull() | ||
internalLink = d.manyHasOne(Url).setNullOnDelete() | ||
externalLink = d.stringColumn() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { SchemaDefinition as d } from '@contember/schema-definition' | ||
import { Article, Category, Tag } from './Article' | ||
|
||
export const UrlType = d.createEnum('article', 'category', 'tag') | ||
|
||
@d.Unique('url') | ||
export class Url { | ||
url = d.stringColumn().notNull() | ||
type = d.enumColumn(UrlType).notNull() | ||
article = d.oneHasOne(Article).cascadeOnDelete() | ||
category = d.oneHasOne(Category).cascadeOnDelete() | ||
tag = d.oneHasOne(Tag).cascadeOnDelete() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/admin-sandbox/src/components/AnchorInsertHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { EditorTransforms, EditorWithBlocks, EntityAccessor } from '@contember/admin' | ||
|
||
export const withAnchorsAsReference = (editor: EditorWithBlocks, { elementType, referenceType = elementType, updateReference }: { | ||
elementType: string, | ||
updateReference: (url: string, getAccessor: () => EntityAccessor) => void | ||
referenceType?: string, | ||
}) => { | ||
const { normalizeNode } = editor | ||
editor.normalizeNode = ([element, path]) => { | ||
if ('type' in element && element.type === 'anchor' && 'href' in element && typeof element.href === 'string') { | ||
const referenceId = editor.createElementReference(path, referenceType, getAccessor => { | ||
updateReference(element.href as string, getAccessor) | ||
}).id | ||
return EditorTransforms.setNodes(editor, { referenceId, href: null, type: elementType }, { at: path }) | ||
} | ||
normalizeNode([element, path]) | ||
} | ||
} |
Oops, something went wrong.