Skip to content

Commit

Permalink
Remove the overrides mechanism.
Browse files Browse the repository at this point in the history
We've managed to augment the types as needed in the Foundation CMS.
  • Loading branch information
microbit-matt-hillsdon committed Mar 26, 2024
1 parent cd0103a commit 472ed66
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 127 deletions.
25 changes: 9 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,21 @@ Add it as a plugin in `sanity.config.ts` (or .js):

```ts
import {defineConfig} from 'sanity'
import {pythonEditor} from '@microbit/sanity-plugin-python-editor-v3'
import {pythonEditor, structure} from '@microbit/sanity-plugin-python-editor-v3'

export default defineConfig({
//...
plugins: [pythonEditor()],
// ...
plugins: [structureTool({structure}), pythonEditor()],
schema: {
types: yourSchemaTypes,
},
})
```

You can pass overrides to the editor that customise the "externalLink", "simpleImage"
and "python" types by passing an alternative simple type definition to the overrides
key of the config. These types must have the same name as the relevant key and be
defined by the app.
Using the exported structure is optional.

```
export default defineConfig({
plugins: [pythonEditor({
overrides: {
python: MyFancyPython
}
})],
})
```
If you need to augment the types defined in this plugin then you can transform them in
your app by providing a [function as the types field](https://www.sanity.io/docs/schema-types).

## Develop & test

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microbit/sanity-plugin-python-editor-v3",
"version": "0.1.0-pre.4",
"version": "0.1.0-pre.7",
"description": "Sanity schemas for the micro:bit Python Editor V3",
"keywords": [
"sanity",
Expand Down
31 changes: 6 additions & 25 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
*
* SPDX-License-Identifier: MIT
*/
import {definePlugin, SchemaTypeDefinition} from 'sanity'
import {definePlugin} from 'sanity'

import {portableTextFactory} from './schemas/portableText'
import portableText from './schemas/portableText'
import python from './schemas/python'
import pythonIdea from './schemas/pythonIdea'
import pythonIdeasConfig from './schemas/pythonIdeasConfig'
Expand All @@ -15,40 +15,23 @@ import simpleImage from './schemas/simpleImage'
import structure from './schemas/structure'
import toolkit from './schemas/toolkit'
import toolkitAlternativeContent from './schemas/toolkitAlternativeContent'
import toolkitApiLink from './schemas/toolkitApiLink'
import toolkitInternalLink from './schemas/toolkitInternalLink'
import toolkitTopic from './schemas/toolkitTopic'
import toolkitTopicEntry from './schemas/toolkitTopicEntry'

export interface PythonEditorPluginConfig {
overrides?: {
externalLink?: SchemaTypeDefinition
simpleImage?: SchemaTypeDefinition
python?: SchemaTypeDefinition
}
}

export const pythonEditor = definePlugin((userConfig: PythonEditorPluginConfig | void) => {
const config = userConfig ?? {}
export const pythonEditor = definePlugin(() => {
const types = [
portableTextFactory(config),
portableText,
pythonIdea,
pythonIdeasConfig,
pythonModule,
pythonModuleItem,
toolkit,
toolkitAlternativeContent,
toolkitApiLink,
toolkitInternalLink,
toolkitTopic,
toolkitTopicEntry,
python,
simpleImage,
]
if (!config.overrides?.python) {
types.push(python)
}
if (!config.overrides?.simpleImage) {
types.push(simpleImage)
}
return {
name: '@microbit/python-editor-v3-sanity',
schema: {
Expand All @@ -64,8 +47,6 @@ export {
structure,
toolkit,
toolkitAlternativeContent,
toolkitApiLink,
toolkitInternalLink,
toolkitTopic,
toolkitTopicEntry,
}
110 changes: 74 additions & 36 deletions src/schemas/portableText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,80 @@
*
* SPDX-License-Identifier: MIT
*/
import {FaFileAlt as textIcon} from 'react-icons/fa'
import {ArrayOfType, defineArrayMember, defineType, SchemaTypeDefinition} from 'sanity'
import {
FaExternalLinkAlt as ExternalLinkIcon,
FaFileAlt as textIcon,
FaPaperclip as linkIcon,
} from 'react-icons/fa'
import {defineArrayMember, defineField, defineType} from 'sanity'

import {PythonEditorPluginConfig} from '..'
import externalLink from './externalLink'
import toolkitApiLink from './toolkitApiLink'
import toolkitInternalLink from './toolkitInternalLink'
import {translatableReferenceOptions} from '../common/translation'

export const portableTextFactory = (config: PythonEditorPluginConfig): SchemaTypeDefinition => {
return defineType({
name: 'toolkitContent',
title: 'toolkitContent',
type: 'array',
icon: textIcon,
of: [
defineArrayMember({
type: 'block',
styles: [
{title: 'Normal', value: 'normal'},
{title: 'H3', value: 'h3'},
const portableText = defineType({
name: 'toolkitContent',
title: 'toolkitContent',
type: 'array',
icon: textIcon,
of: [
defineArrayMember({
type: 'block',
styles: [
{title: 'Normal', value: 'normal'},
{title: 'H3', value: 'h3'},
],
marks: {
decorators: [
{title: 'Strong', value: 'strong'},
{title: 'Code', value: 'code'},
],
marks: {
decorators: [
{title: 'Strong', value: 'strong'},
{title: 'Code', value: 'code'},
],
annotations: [
toolkitInternalLink,
toolkitApiLink,
config.overrides?.externalLink ?? externalLink,
// There seems to be an issue with validation-related types here, hence the kludge.
] as unknown as ArrayOfType<'object' | 'reference', undefined>[],
},
}),
{type: 'python'},
{type: 'simpleImage'},
],
})
}
annotations: [
{
name: 'toolkitApiLink',
type: 'object',
title: 'Python API link',
icon: linkIcon,
fields: [
defineField({
name: 'name',
description: 'Fully qualified name, e.g. microbit.compass.get_x',
type: 'string',
}),
],
},
{
name: 'toolkitInternalLink',
type: 'object',
title: 'Internal link',
icon: linkIcon,
fields: [
defineField({
name: 'reference',
type: 'reference',
options: translatableReferenceOptions,
to: [{type: 'toolkitTopicEntry'}, {type: 'toolkitTopic'}],
}),
],
},
{
name: 'link',
type: 'object',
title: 'External link',
icon: ExternalLinkIcon,
fields: [
defineField({
title: 'URL',
name: 'href',
type: 'url',
validation: (rule) => rule.required(),
}),
],
},
],
},
}),
{type: 'python'},
{type: 'simpleImage'},
],
})

export default portableText
23 changes: 0 additions & 23 deletions src/schemas/toolkitApiLink.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/schemas/toolkitInternalLink.ts

This file was deleted.

0 comments on commit 472ed66

Please sign in to comment.