Skip to content

Commit

Permalink
Merge branch 'fer/1832' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolucchesi committed Jan 18, 2024
2 parents 9cd79a1 + b41ab06 commit 1aa5aed
Show file tree
Hide file tree
Showing 18 changed files with 526 additions and 193 deletions.
1 change: 1 addition & 0 deletions sanityv3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"@types/react-dom": "^18.0.5",
"@types/styled-components": "^5.1.26",
"eslint": "^8.6.0",
"nanoid": "^5.0.4",
"prettier": "^2.8.3",
"typescript": "^4.0.0"
}
Expand Down
10 changes: 10 additions & 0 deletions sanityv3/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 17 additions & 1 deletion sanityv3/schemas/editors/blockContentType.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { attach_file, external_link, link } from '@equinor/eds-icons'
import { attach_file, external_link, format_color_text, link } from '@equinor/eds-icons'
import type { BlockDefinition, Rule, ValidationContext } from 'sanity'
import { filterByPages, filterByPagesInOtherLanguages } from '../../helpers/referenceFilters'
import { EdsBlockEditorIcon, EdsIcon, IconSubScript, IconSuperScript } from '../../icons'
Expand All @@ -16,12 +16,23 @@ export type BlockContentProps = {
attachment?: boolean
lists?: boolean
smallText?: boolean
highlight?: boolean
normalTextOverride?: {
title: string
value: 'normal'
component?: ({ children }: { children: React.ReactNode }) => JSX.Element
}
}

export const textColorConfig = {
title: 'Highlight',
value: 'highlight',
icon: EdsBlockEditorIcon(format_color_text),
component: ({ children }: { children: React.ReactNode }) => {
return <span style={{ color: 'hsl(348, 100%, 54%)' }}>{children}</span>
},
}

const SmallTextRender = (props: any) => {
const { children } = props
return <span style={{ fontSize: '0.8rem' }}>{children}</span>
Expand All @@ -38,6 +49,7 @@ export const configureBlockContent = (options: BlockContentProps = {}): BlockDef
attachment = false,
lists = true,
smallText = true,
highlight = false,
normalTextOverride = { title: 'Normal', value: 'normal' },
} = options

Expand Down Expand Up @@ -238,6 +250,10 @@ export const configureBlockContent = (options: BlockContentProps = {}): BlockDef
config?.marks?.annotations?.push(attachmentConfig)
}

if (highlight) {
config.marks?.decorators?.push(textColorConfig)
}

return config
}

Expand Down
14 changes: 2 additions & 12 deletions sanityv3/schemas/editors/titleEditorContentType.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { SuperScriptRenderer, SubScriptRenderer, StrikeThroughRenderer } from '../components'
import { IconSuperScript, IconSubScript, EdsBlockEditorIcon } from '../../icons'
import { IconSuperScript, IconSubScript } from '../../icons'
import { StrikethroughIcon } from '@sanity/icons'
import { BlockDefinition, BlockStyleDefinition } from 'sanity'
import { format_color_text } from '@equinor/eds-icons'
import React from 'react'
import { textColorConfig } from './blockContentType'

export type TitleContentProps = {
styles?: BlockStyleDefinition[]
Expand Down Expand Up @@ -55,15 +54,6 @@ export const configureTitleBlockContent = (
},
}

const textColorConfig = {
title: 'Highlight',
value: 'highlight',
icon: EdsBlockEditorIcon(format_color_text),
component: ({ children }: { children: React.ReactNode }) => {
return <span style={{ color: 'hsl(348, 100%, 54%)' }}>{children}</span>
},
}

if (highlight) {
config.marks?.decorators?.push(textColorConfig)
}
Expand Down
2 changes: 2 additions & 0 deletions sanityv3/schemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import simpleMenuGroup from './objects/simpleMenuGroup'
import simpleMenuLink from './objects/simpleMenuLink'
import stockValuesApi from './objects/stockValuesApi'
import table from './objects/table'
import tableRichText from './objects/table/tableRichText'
import teaser from './objects/teaser'
import textBlock from './objects/textBlock'
import textWithIcon from './objects/textWithIcon'
Expand Down Expand Up @@ -144,6 +145,7 @@ const RemainingSchemas = [
promotePeople,
contactList,
table,
tableRichText,
assetFile,
assetTag,
cookieDeclaration,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
import blocksToText from '../../helpers/blocksToText'
import { defaultColors } from '../components/ColorSelector'
import CompactBlockEditor from '../components/CompactBlockEditor'
import { configureBlockContent, configureTitleBlockContent } from '../editors'
import blocksToText from '../../../helpers/blocksToText'
import { defaultColors } from '../../components/ColorSelector'
import CompactBlockEditor from '../../components/CompactBlockEditor'
import { configureBlockContent, configureTitleBlockContent } from '../../editors'

import { PortableTextBlock, Rule } from 'sanity'
import type { ColorSelectorValue } from '../components/ColorSelector'
import { EdsIcon } from '../../icons'
import type { ColorSelectorValue } from '../../components/ColorSelector'
import { EdsIcon } from '../../../icons'
import { table_chart } from '@equinor/eds-icons'

export type Table = {
_type: 'promotion'
_type: 'table'
title?: PortableTextBlock[]
ingress?: PortableTextBlock[]
background?: ColorSelectorValue
theme?: ColorType
}

const titleContentType = configureTitleBlockContent()

type ColorType = {
title: string
value: string
}

const themes: ColorType[] = [
{
title: 'Grey',
value: '#e7e7e7',
},
{
title: 'Blue',
value: '#A8C3DB',
},
{
title: 'Green',
value: '#C2D4D6',
},
]

const ingressContentType = configureBlockContent({
h1: false,
h2: false,
Expand All @@ -36,15 +57,7 @@ const headerCellContentType = configureBlockContent({
lists: false,
smallText: false,
})
/* const tableCellContentType = configureBlockContent({
h1: false,
h2: false,
h3: false,
h4: false,
lists: false,
attachment: true,
})
*/

const chosenColors = ['White', 'Mid Green', 'Moss Green Light', 'Spruce Wood', 'Mist Blue']
const backgroundColors = defaultColors.filter((color) => chosenColors.includes(color.title))
export default {
Expand Down Expand Up @@ -113,10 +126,9 @@ export default {
{ type: 'linkSelector', title: 'Link' },
{ type: 'downloadableFile', title: 'Downloadable file' },
{
type: 'object',
title: 'Text Field',
name: 'textField',
fields: [{ type: 'string', name: 'text', title: 'Text' }],
title: 'RichText',
name: 'richText',
type: 'tableRichText',
},
{
type: 'object',
Expand All @@ -134,7 +146,6 @@ export default {
},
],
},

{
type: 'object',
title: 'Number Field',
Expand All @@ -157,14 +168,14 @@ export default {
prepare({ cells }: { cells: any[] }) {
const [cellOne, cellTwo, cellThree, cellFour] = cells
const numberOfCells = cells.length

const getText = (cellContent: any) => {
if (cellContent._type === 'linkSelector') {
return cellContent.label
} else if (cellContent._type === 'downloadableFile') {
return cellContent.filename
} else if (cellContent._type === 'textField') {
return cellContent.text
} else if (cellContent._type === 'richText') {
const cellString = blocksToText(cellContent.text) || ''
return cellString.length > 15 ? cellString.slice(0, 15) + '...' : cellString
} else if (cellContent._type === 'dateField') {
return cellContent.date
}
Expand All @@ -187,6 +198,17 @@ export default {
},
],
},
{
title: 'Table theme',
description: 'Pick a theme for the table. Default is grey.',
name: 'theme',
type: 'colorlist',
options: {
colors: themes,
},
initialValue: themes[0],
fieldset: 'design',
},
{
title: 'Background',
description: 'Pick a colour for the background. Default is white.',
Expand Down
26 changes: 26 additions & 0 deletions sanityv3/schemas/objects/table/tableRichText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { configureBlockContent } from '../../editors'

const richTextType = configureBlockContent({
h2: false,
h3: false,
externalLink: false,
internalLink: false,
lists: false,
attachment: false,
smallText: false,
highlight: true,
})

export default {
title: 'Rich Text',
name: 'tableRichText',
type: 'object',
fields: [
{
name: 'text',
title: 'Text',
type: 'array',
of: [richTextType],
},
],
}
15 changes: 11 additions & 4 deletions sanityv3/scripts/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Description
# README.md

This folder contains scripts used to migrate content.
1. Set up the datasets in which the script should be run in the `./run.ts` file;

- Change the target scripts array with the desired migration scripts.
- `npx sanity exec 'automateScripts.mjs'` to run the migration scripts.
2. Set up the scripts that should be run in the `./run.ts` file;

3. Make sure you have Sanity CLI setup on your terminal

4. Login into Sanity CLI using `sanity login --sso=equinor-sanity`

5. Go to the script folders using `cd sanityv3/scripts`

6. Run with `sanity exec run.js --with-user-token`;
93 changes: 93 additions & 0 deletions sanityv3/scripts/issue-1832/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { customAlphabet } from 'nanoid'

export default function script(client) {
const nanoid = customAlphabet('1234567890abcdef', 12)

const fetchDocuments = async () =>
client.fetch(/* groq */ `
*[_type == 'page' &&
(count(content[_type == "table"].tableRows[count(row[_type == 'textField']) > 0]) > 0)
] [0...100] {
_id,
_rev,
"table": content[_type == 'table' && count(tableRows[count(row[_type == 'textField']) > 0]) > 0] {
"tableRows": tableRows[count(row[_type == 'textField']) > 0]{
"row": row[_type == 'textField']
}
}
}
`)

const buildPatches = (docs) =>
docs
.map((doc) =>
doc.fields.map((el) => ({
id: doc.id,
patch: {
set: {
[`content..[_key == "${el._key}"]`]: {
_key: el._key,
_type: 'richText',
text: [
{
_key: nanoid(),
style: 'normal',
_type: 'block',
children: [
{
_key: nanoid(),
_type: 'span',
marks: [],
text: el.text,
},
],
markDefs: [],
},
],
},
},
// this will cause the migration to fail if any of the documents has been
// modified since it was fetched.
ifRevisionID: doc._rev,
},
})),
)
.flat()

const createTransaction = (patches) =>
patches.reduce((tx, patch) => tx.patch(patch.id, patch.patch), client.transaction())

const commitTransaction = (tx) => tx.commit()

const migrateNextBatch = async () => {
console.log('fetching...')
const rawDocuments = await fetchDocuments()
const documents = rawDocuments.map((document) => {
return {
id: document._id,
fields: document.table?.flatMap((content) =>
content.tableRows?.flatMap((table) => table.row?.flatMap((text) => text)),
),
}
})

const patches = buildPatches(documents)

if (patches.length === 0) {
console.log('No more documents to migrate!')
return null
}
console.log(
`Migrating batch:\n %s`,
patches.map((patch) => `${patch.id} => ${JSON.stringify(patch.patch)}`).join('\n'),
)
const transaction = createTransaction(patches)
await commitTransaction(transaction)
return migrateNextBatch()
}

migrateNextBatch().catch((err) => {
console.error(err)
process.exit(1)
})
}
Loading

0 comments on commit 1aa5aed

Please sign in to comment.