@ainsleydev/payload-helper 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/types.js +1 -2
- package/dist/cli/types.js.map +1 -1
- package/dist/collections/Media.js +7 -13
- package/dist/collections/Media.js.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/dist/jest.config.js +27 -0
- package/dist/jest.config.js.map +1 -0
- package/dist/util/lexical.js +53 -0
- package/dist/util/lexical.js.map +1 -0
- package/dist/util/lexical.test.js +21 -0
- package/dist/util/lexical.test.js.map +1 -0
- package/package.json +1 -1
package/dist/cli/types.js
CHANGED
|
@@ -9,7 +9,7 @@ import { findConfig, importConfig } from 'payload/node';
|
|
|
9
9
|
try {
|
|
10
10
|
configPath = findConfig();
|
|
11
11
|
} catch (e) {
|
|
12
|
-
console.log(
|
|
12
|
+
console.log(`Error finding config: ${e}`);
|
|
13
13
|
return;
|
|
14
14
|
}
|
|
15
15
|
// Set the environment variable to generate Golang types.
|
|
@@ -20,7 +20,6 @@ import { findConfig, importConfig } from 'payload/node';
|
|
|
20
20
|
config,
|
|
21
21
|
disableDBConnect: true,
|
|
22
22
|
disableOnInit: true,
|
|
23
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
24
23
|
// @ts-ignore
|
|
25
24
|
local: true,
|
|
26
25
|
secret: '--unused--'
|
package/dist/cli/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/cli/types.ts"],"sourcesContent":["import * as fs from 'node:fs';\nimport { configToJSONSchema, getPayload } from 'payload';\nimport { findConfig, importConfig } from 'payload/node';\n\n/**\n * Creates JSON schema types of Payloads Collections & Globals\n */\nexport async function generateTypes(outFile: string): Promise<void> {\n\tconsole.log('Compiling JSON types for Collections and Globals...');\n\n\tlet configPath = '';\n\ttry {\n\t\tconfigPath = findConfig();\n\t} catch (e) {\n\t\tconsole.log(
|
|
1
|
+
{"version":3,"sources":["../../src/cli/types.ts"],"sourcesContent":["import * as fs from 'node:fs';\nimport { configToJSONSchema, getPayload } from 'payload';\nimport { findConfig, importConfig } from 'payload/node';\n\n/**\n * Creates JSON schema types of Payloads Collections & Globals\n */\nexport async function generateTypes(outFile: string): Promise<void> {\n\tconsole.log('Compiling JSON types for Collections and Globals...');\n\n\tlet configPath = '';\n\ttry {\n\t\tconfigPath = findConfig();\n\t} catch (e) {\n\t\tconsole.log(`Error finding config: ${e}`);\n\t\treturn;\n\t}\n\n\t// Set the environment variable to generate Golang types.\n\tprocess.env.GEN_GOLANG = 'true';\n\n\tconst config = await importConfig(configPath);\n\tconst outputFile = (process.env.PAYLOAD_TS_OUTPUT_PATH || config.typescript.outputFile).replace(\n\t\t'.ts',\n\t\t'.json',\n\t);\n\n\tconst payload = await getPayload({\n\t\tconfig,\n\t\tdisableDBConnect: true,\n\t\tdisableOnInit: true,\n\t\t// @ts-ignore\n\t\tlocal: true,\n\t\tsecret: '--unused--',\n\t});\n\n\tconst jsonSchema = configToJSONSchema(payload.config, payload.db.defaultIDType);\n\tconst prettyJSON = JSON.stringify(jsonSchema, null, 4);\n\n\tfs.writeFileSync(outputFile, prettyJSON);\n\n\tconsole.log(`JSON types written to: ${outputFile}`);\n\n\tdelete process.env.GEN_GOLANG;\n}\n"],"names":["fs","configToJSONSchema","getPayload","findConfig","importConfig","generateTypes","outFile","console","log","configPath","e","process","env","GEN_GOLANG","config","outputFile","PAYLOAD_TS_OUTPUT_PATH","typescript","replace","payload","disableDBConnect","disableOnInit","local","secret","jsonSchema","db","defaultIDType","prettyJSON","JSON","stringify","writeFileSync"],"mappings":"AAAA,YAAYA,QAAQ,UAAU;AAC9B,SAASC,kBAAkB,EAAEC,UAAU,QAAQ,UAAU;AACzD,SAASC,UAAU,EAAEC,YAAY,QAAQ,eAAe;AAExD;;CAEC,GACD,OAAO,eAAeC,cAAcC,OAAe;IAClDC,QAAQC,GAAG,CAAC;IAEZ,IAAIC,aAAa;IACjB,IAAI;QACHA,aAAaN;IACd,EAAE,OAAOO,GAAG;QACXH,QAAQC,GAAG,CAAC,CAAC,sBAAsB,EAAEE,EAAE,CAAC;QACxC;IACD;IAEA,yDAAyD;IACzDC,QAAQC,GAAG,CAACC,UAAU,GAAG;IAEzB,MAAMC,SAAS,MAAMV,aAAaK;IAClC,MAAMM,aAAa,AAACJ,CAAAA,QAAQC,GAAG,CAACI,sBAAsB,IAAIF,OAAOG,UAAU,CAACF,UAAU,AAAD,EAAGG,OAAO,CAC9F,OACA;IAGD,MAAMC,UAAU,MAAMjB,WAAW;QAChCY;QACAM,kBAAkB;QAClBC,eAAe;QACf,aAAa;QACbC,OAAO;QACPC,QAAQ;IACT;IAEA,MAAMC,aAAavB,mBAAmBkB,QAAQL,MAAM,EAAEK,QAAQM,EAAE,CAACC,aAAa;IAC9E,MAAMC,aAAaC,KAAKC,SAAS,CAACL,YAAY,MAAM;IAEpDxB,GAAG8B,aAAa,CAACf,YAAYY;IAE7BpB,QAAQC,GAAG,CAAC,CAAC,uBAAuB,EAAEO,WAAW,CAAC;IAElD,OAAOJ,QAAQC,GAAG,CAACC,UAAU;AAC9B"}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// import type {
|
|
2
|
+
// LexicalRichTextAdapterProvider,
|
|
3
|
+
// LexicalEditorProps,
|
|
4
|
+
// } from '@payloadcms/richtext-lexical';
|
|
1
5
|
/**
|
|
2
6
|
* Media Collection Configuration
|
|
3
7
|
* Additional fields will be appended to the media collection.
|
|
@@ -5,7 +9,8 @@
|
|
|
5
9
|
* @constructor
|
|
6
10
|
* @param editor
|
|
7
11
|
* @param additionalFields
|
|
8
|
-
*/ export const Media = (editor
|
|
12
|
+
*/ export const Media = (//editor?: (props?: LexicalEditorProps) => LexicalRichTextAdapterProvider,
|
|
13
|
+
additionalFields)=>{
|
|
9
14
|
return {
|
|
10
15
|
slug: 'media',
|
|
11
16
|
access: {
|
|
@@ -20,18 +25,7 @@
|
|
|
20
25
|
{
|
|
21
26
|
name: 'caption',
|
|
22
27
|
type: 'richText',
|
|
23
|
-
required: false
|
|
24
|
-
editor: editor({
|
|
25
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
26
|
-
// @ts-ignore
|
|
27
|
-
features: ({ defaultFeatures })=>{
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
29
|
-
// @ts-ignore
|
|
30
|
-
return defaultFeatures.filter((feature)=>{
|
|
31
|
-
return feature.key === 'paragraph' || feature.key === 'link';
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
})
|
|
28
|
+
required: false
|
|
35
29
|
},
|
|
36
30
|
...additionalFields ? additionalFields : []
|
|
37
31
|
],
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/collections/Media.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload';\n
|
|
1
|
+
{"version":3,"sources":["../../src/collections/Media.ts"],"sourcesContent":["import type { CollectionConfig, Field } from 'payload';\n// import type {\n// \tLexicalRichTextAdapterProvider,\n// \tLexicalEditorProps,\n// } from '@payloadcms/richtext-lexical';\n\n/**\n * Media Collection Configuration\n * Additional fields will be appended to the media collection.\n *\n * @constructor\n * @param editor\n * @param additionalFields\n */\nexport const Media = (\n\t//editor?: (props?: LexicalEditorProps) => LexicalRichTextAdapterProvider,\n\tadditionalFields?: Field[],\n): CollectionConfig => {\n\treturn {\n\t\tslug: 'media',\n\t\taccess: {\n\t\t\tread: () => true,\n\t\t},\n\t\tfields: [\n\t\t\t{\n\t\t\t\tname: 'alt',\n\t\t\t\ttype: 'text',\n\t\t\t\trequired: true,\n\t\t\t},\n\t\t\t{\n\t\t\t\tname: 'caption',\n\t\t\t\ttype: 'richText',\n\t\t\t\trequired: false,\n\t\t\t\t// editor: editor({\n\t\t\t\t// \t// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t\t\t// \t// @ts-ignore\n\t\t\t\t// \tfeatures: ({ defaultFeatures }) => {\n\t\t\t\t// \t\t// eslint-disable-next-line @typescript-eslint/ban-ts-comment\n\t\t\t\t// \t\t// @ts-ignore\n\t\t\t\t// \t\treturn defaultFeatures.filter((feature) => {\n\t\t\t\t// \t\t\treturn feature.key === 'paragraph' || feature.key === 'link';\n\t\t\t\t// \t\t});\n\t\t\t\t// \t},\n\t\t\t\t// }),\n\t\t\t},\n\t\t\t...(additionalFields ? additionalFields : []),\n\t\t],\n\t\tupload: {\n\t\t\tstaticDir: 'media',\n\t\t\timageSizes: [\n\t\t\t\t// Original Size (for WebP & Avif)\n\t\t\t\t{\n\t\t\t\t\tname: 'webp',\n\t\t\t\t\twidth: undefined,\n\t\t\t\t\theight: undefined,\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'webp',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: 'avif',\n\t\t\t\t\twidth: undefined,\n\t\t\t\t\theight: undefined,\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'avif',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t// Thumbnail Sizes\n\t\t\t\t{\n\t\t\t\t\tname: 'thumbnail',\n\t\t\t\t\twidth: 400,\n\t\t\t\t\theight: 300,\n\t\t\t\t\tposition: 'centre',\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: 'thumbnail_webp',\n\t\t\t\t\twidth: 400,\n\t\t\t\t\theight: 300,\n\t\t\t\t\tposition: 'centre',\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'webp',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: 'thumbnail_avif',\n\t\t\t\t\twidth: 400,\n\t\t\t\t\theight: 300,\n\t\t\t\t\tposition: 'centre',\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'avif',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t// Mobile Sizes\n\t\t\t\t{\n\t\t\t\t\tname: 'mobile',\n\t\t\t\t\twidth: 768,\n\t\t\t\t\theight: undefined,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: 'mobile_webp',\n\t\t\t\t\twidth: 768,\n\t\t\t\t\theight: undefined,\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'webp',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: 'mobile_avif',\n\t\t\t\t\twidth: 768,\n\t\t\t\t\theight: undefined,\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'avif',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t// Tablet Sizes\n\t\t\t\t{\n\t\t\t\t\tname: 'tablet',\n\t\t\t\t\twidth: 1024,\n\t\t\t\t\theight: undefined,\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: 'tablet_webp',\n\t\t\t\t\twidth: 1024,\n\t\t\t\t\theight: undefined,\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'webp',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\t{\n\t\t\t\t\tname: 'tablet_avif',\n\t\t\t\t\twidth: 1024,\n\t\t\t\t\theight: undefined,\n\t\t\t\t\tformatOptions: {\n\t\t\t\t\t\tformat: 'avif',\n\t\t\t\t\t\toptions: {\n\t\t\t\t\t\t\tquality: 80,\n\t\t\t\t\t\t},\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t],\n\t\t},\n\t};\n};\n"],"names":["Media","additionalFields","slug","access","read","fields","name","type","required","upload","staticDir","imageSizes","width","undefined","height","formatOptions","format","options","quality","position"],"mappings":"AACA,gBAAgB;AAChB,mCAAmC;AACnC,uBAAuB;AACvB,yCAAyC;AAEzC;;;;;;;CAOC,GACD,OAAO,MAAMA,QAAQ,CACpB,0EAA0E;AAC1EC;IAEA,OAAO;QACNC,MAAM;QACNC,QAAQ;YACPC,MAAM,IAAM;QACb;QACAC,QAAQ;YACP;gBACCC,MAAM;gBACNC,MAAM;gBACNC,UAAU;YACX;YACA;gBACCF,MAAM;gBACNC,MAAM;gBACNC,UAAU;YAYX;eACIP,mBAAmBA,mBAAmB,EAAE;SAC5C;QACDQ,QAAQ;YACPC,WAAW;YACXC,YAAY;gBACX,kCAAkC;gBAClC;oBACCL,MAAM;oBACNM,OAAOC;oBACPC,QAAQD;oBACRE,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;gBACA;oBACCZ,MAAM;oBACNM,OAAOC;oBACPC,QAAQD;oBACRE,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;gBACA,kBAAkB;gBAClB;oBACCZ,MAAM;oBACNM,OAAO;oBACPE,QAAQ;oBACRK,UAAU;gBACX;gBACA;oBACCb,MAAM;oBACNM,OAAO;oBACPE,QAAQ;oBACRK,UAAU;oBACVJ,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;gBACA;oBACCZ,MAAM;oBACNM,OAAO;oBACPE,QAAQ;oBACRK,UAAU;oBACVJ,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;gBACA,eAAe;gBACf;oBACCZ,MAAM;oBACNM,OAAO;oBACPE,QAAQD;gBACT;gBACA;oBACCP,MAAM;oBACNM,OAAO;oBACPE,QAAQD;oBACRE,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;gBACA;oBACCZ,MAAM;oBACNM,OAAO;oBACPE,QAAQD;oBACRE,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;gBACA,eAAe;gBACf;oBACCZ,MAAM;oBACNM,OAAO;oBACPE,QAAQD;gBACT;gBACA;oBACCP,MAAM;oBACNM,OAAO;oBACPE,QAAQD;oBACRE,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;gBACA;oBACCZ,MAAM;oBACNM,OAAO;oBACPE,QAAQD;oBACRE,eAAe;wBACdC,QAAQ;wBACRC,SAAS;4BACRC,SAAS;wBACV;oBACD;gBACD;aACA;QACF;IACD;AACD,EAAE"}
|
package/dist/index.js
CHANGED
|
@@ -6,7 +6,6 @@ import env from './util/env';
|
|
|
6
6
|
* @constructor
|
|
7
7
|
* @param pluginOptions
|
|
8
8
|
*/ export const payloadHelper = (pluginOptions)=>(incomingConfig)=>{
|
|
9
|
-
console.log(pluginOptions);
|
|
10
9
|
const genGoLang = env.bool('GEN_GOLANG', false);
|
|
11
10
|
if (genGoLang) {
|
|
12
11
|
incomingConfig.typescript = {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config } from 'payload';\nimport { fieldMapper, schemas } from './plugin/schema';\nimport env from './util/env';\n\n/**\n * Plugin Options\n */\nexport interface PluginOptions {\n\tSEOFields?: boolean;\n}\n\n/**\n * Payload Helper Plugin for websites at ainsley.dev\n *\n * @constructor\n * @param pluginOptions\n */\nexport const payloadHelper =\n\t(pluginOptions:
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { Config } from 'payload';\nimport { fieldMapper, schemas } from './plugin/schema';\nimport env from './util/env';\n\n/**\n * Plugin Options\n */\nexport interface PluginOptions {\n\tSEOFields?: boolean;\n}\n\n/**\n * Payload Helper Plugin for websites at ainsley.dev\n *\n * @constructor\n * @param pluginOptions\n */\nexport const payloadHelper =\n\t(pluginOptions: PluginOptions) =>\n\t(incomingConfig: Config): Config => {\n\t\tconst genGoLang = env.bool('GEN_GOLANG', false);\n\t\tif (genGoLang) {\n\t\t\tincomingConfig.typescript = {\n\t\t\t\t...incomingConfig.typescript,\n\t\t\t\tschema: schemas,\n\t\t\t};\n\t\t\tincomingConfig = fieldMapper(incomingConfig);\n\t\t}\n\n\t\tincomingConfig.typescript = incomingConfig.typescript || {};\n\t\tincomingConfig.typescript.outputFile = './src/types/payload.ts';\n\n\t\treturn incomingConfig;\n\t};\n"],"names":["fieldMapper","schemas","env","payloadHelper","pluginOptions","incomingConfig","genGoLang","bool","typescript","schema","outputFile"],"mappings":"AACA,SAASA,WAAW,EAAEC,OAAO,QAAQ,kBAAkB;AACvD,OAAOC,SAAS,aAAa;AAS7B;;;;;CAKC,GACD,OAAO,MAAMC,gBACZ,CAACC,gBACD,CAACC;QACA,MAAMC,YAAYJ,IAAIK,IAAI,CAAC,cAAc;QACzC,IAAID,WAAW;YACdD,eAAeG,UAAU,GAAG;gBAC3B,GAAGH,eAAeG,UAAU;gBAC5BC,QAAQR;YACT;YACAI,iBAAiBL,YAAYK;QAC9B;QAEAA,eAAeG,UAAU,GAAGH,eAAeG,UAAU,IAAI,CAAC;QAC1DH,eAAeG,UAAU,CAACE,UAAU,GAAG;QAEvC,OAAOL;IACR,EAAE"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
verbose: true,
|
|
3
|
+
preset: 'ts-jest/presets/default-esm',
|
|
4
|
+
testEnvironment: 'node',
|
|
5
|
+
transform: {
|
|
6
|
+
'^.+\\.tsx?$': 'ts-jest'
|
|
7
|
+
},
|
|
8
|
+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.ts?$',
|
|
9
|
+
moduleFileExtensions: [
|
|
10
|
+
'ts',
|
|
11
|
+
'tsx',
|
|
12
|
+
'js',
|
|
13
|
+
'jsx',
|
|
14
|
+
'json',
|
|
15
|
+
'node'
|
|
16
|
+
],
|
|
17
|
+
extensionsToTreatAsEsm: [
|
|
18
|
+
'.ts'
|
|
19
|
+
],
|
|
20
|
+
globals: {
|
|
21
|
+
'ts-jest': {
|
|
22
|
+
useESM: true
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=jest.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/jest.config.mjs"],"sourcesContent":["export default {\n\tverbose: true,\n\tpreset: 'ts-jest/presets/default-esm',\n\ttestEnvironment: 'node',\n\ttransform: {\n\t\t'^.+\\\\.tsx?$': 'ts-jest',\n\t},\n\ttestRegex: '(/__tests__/.*|(\\\\.|/)(test|spec))\\\\.ts?$',\n\tmoduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],\n\n\textensionsToTreatAsEsm: ['.ts'],\n\tglobals: {\n\t\t'ts-jest': {\n\t\t\tuseESM: true,\n\t\t},\n\t},\n};\n\n"],"names":["verbose","preset","testEnvironment","transform","testRegex","moduleFileExtensions","extensionsToTreatAsEsm","globals","useESM"],"mappings":"AAAA,eAAe;IACdA,SAAS;IACTC,QAAQ;IACRC,iBAAiB;IACjBC,WAAW;QACV,eAAe;IAChB;IACAC,WAAW;IACXC,sBAAsB;QAAC;QAAM;QAAO;QAAM;QAAO;QAAQ;KAAO;IAEhEC,wBAAwB;QAAC;KAAM;IAC/BC,SAAS;QACR,WAAW;YACVC,QAAQ;QACT;IACD;AACD,EAAE"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { createHeadlessEditor } from '@lexical/headless';
|
|
2
|
+
import { $generateNodesFromDOM, $generateHtmlFromNodes } from '@lexical/html';
|
|
3
|
+
import { $getRoot, $getSelection } from 'lexical';
|
|
4
|
+
import { JSDOM } from 'jsdom';
|
|
5
|
+
const editor = createHeadlessEditor({
|
|
6
|
+
nodes: [],
|
|
7
|
+
onError: ()=>{}
|
|
8
|
+
});
|
|
9
|
+
/**
|
|
10
|
+
* Converts an HTML string to a Lexical editor state.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} html - The HTML string to convert.
|
|
13
|
+
* @returns {SerializedEditorState} The serialized editor state.
|
|
14
|
+
*/ export const htmlToLexical = (html)=>{
|
|
15
|
+
editor.update(()=>{
|
|
16
|
+
// In a headless environment you can use a package such as JSDom to parse the HTML string.
|
|
17
|
+
const dom = new JSDOM(html);
|
|
18
|
+
// Once you have the DOM instance it's easy to generate LexicalNodes.
|
|
19
|
+
const nodes = $generateNodesFromDOM(editor, dom.window.document);
|
|
20
|
+
// Select the root
|
|
21
|
+
$getRoot().select();
|
|
22
|
+
// Insert them at a selection.
|
|
23
|
+
const selection = $getSelection();
|
|
24
|
+
if (selection) selection.insertNodes(nodes);
|
|
25
|
+
}, {
|
|
26
|
+
discrete: true
|
|
27
|
+
});
|
|
28
|
+
return editor.getEditorState().toJSON();
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Converts a Lexical editor state to an HTML string.
|
|
32
|
+
*
|
|
33
|
+
* @param {SerializedEditorState} json - The serialized editor state to convert.
|
|
34
|
+
* @returns {string} The HTML string.
|
|
35
|
+
*/ export const lexicalToHtml = (json)=>{
|
|
36
|
+
// Initialize a JSDOM instance
|
|
37
|
+
const dom = new JSDOM('');
|
|
38
|
+
// @ts-ignore
|
|
39
|
+
globalThis.window = dom.window;
|
|
40
|
+
globalThis.document = dom.window.document;
|
|
41
|
+
editor.update(()=>{
|
|
42
|
+
const editorState = editor.parseEditorState(json);
|
|
43
|
+
editor.setEditorState(editorState);
|
|
44
|
+
});
|
|
45
|
+
// Convert the editor state to HTML
|
|
46
|
+
let html = '';
|
|
47
|
+
editor.getEditorState().read(()=>{
|
|
48
|
+
html = $generateHtmlFromNodes(editor);
|
|
49
|
+
});
|
|
50
|
+
return html;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//# sourceMappingURL=lexical.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/util/lexical.ts"],"sourcesContent":["import { createHeadlessEditor } from '@lexical/headless';\nimport { $generateNodesFromDOM, $generateHtmlFromNodes } from '@lexical/html';\nimport { $getRoot, $getSelection } from 'lexical';\nimport { JSDOM } from 'jsdom';\nimport type { SerializedEditorState } from 'lexical';\n\nconst editor = createHeadlessEditor({\n\tnodes: [],\n\tonError: () => {},\n});\n\n/**\n * Converts an HTML string to a Lexical editor state.\n *\n * @param {string} html - The HTML string to convert.\n * @returns {SerializedEditorState} The serialized editor state.\n */\nexport const htmlToLexical = (html: string): SerializedEditorState => {\n\teditor.update(\n\t\t() => {\n\t\t\t// In a headless environment you can use a package such as JSDom to parse the HTML string.\n\t\t\tconst dom = new JSDOM(html);\n\n\t\t\t// Once you have the DOM instance it's easy to generate LexicalNodes.\n\t\t\tconst nodes = $generateNodesFromDOM(editor, dom.window.document);\n\n\t\t\t// Select the root\n\t\t\t$getRoot().select();\n\n\t\t\t// Insert them at a selection.\n\t\t\tconst selection = $getSelection();\n\n\t\t\tif (selection) selection.insertNodes(nodes);\n\t\t},\n\t\t{ discrete: true },\n\t);\n\n\treturn editor.getEditorState().toJSON();\n};\n\n/**\n * Converts a Lexical editor state to an HTML string.\n *\n * @param {SerializedEditorState} json - The serialized editor state to convert.\n * @returns {string} The HTML string.\n */\nexport const lexicalToHtml = (json: SerializedEditorState): string => {\n\t// Initialize a JSDOM instance\n\tconst dom = new JSDOM('');\n\n\t// @ts-ignore\n\tglobalThis.window = dom.window;\n\tglobalThis.document = dom.window.document;\n\n\teditor.update(() => {\n\t\tconst editorState = editor.parseEditorState(json);\n\t\teditor.setEditorState(editorState);\n\t});\n\n\t// Convert the editor state to HTML\n\tlet html = '';\n\teditor.getEditorState().read(() => {\n\t\thtml = $generateHtmlFromNodes(editor);\n\t});\n\n\treturn html;\n};\n"],"names":["createHeadlessEditor","$generateNodesFromDOM","$generateHtmlFromNodes","$getRoot","$getSelection","JSDOM","editor","nodes","onError","htmlToLexical","html","update","dom","window","document","select","selection","insertNodes","discrete","getEditorState","toJSON","lexicalToHtml","json","globalThis","editorState","parseEditorState","setEditorState","read"],"mappings":"AAAA,SAASA,oBAAoB,QAAQ,oBAAoB;AACzD,SAASC,qBAAqB,EAAEC,sBAAsB,QAAQ,gBAAgB;AAC9E,SAASC,QAAQ,EAAEC,aAAa,QAAQ,UAAU;AAClD,SAASC,KAAK,QAAQ,QAAQ;AAG9B,MAAMC,SAASN,qBAAqB;IACnCO,OAAO,EAAE;IACTC,SAAS,KAAO;AACjB;AAEA;;;;;CAKC,GACD,OAAO,MAAMC,gBAAgB,CAACC;IAC7BJ,OAAOK,MAAM,CACZ;QACC,0FAA0F;QAC1F,MAAMC,MAAM,IAAIP,MAAMK;QAEtB,qEAAqE;QACrE,MAAMH,QAAQN,sBAAsBK,QAAQM,IAAIC,MAAM,CAACC,QAAQ;QAE/D,kBAAkB;QAClBX,WAAWY,MAAM;QAEjB,8BAA8B;QAC9B,MAAMC,YAAYZ;QAElB,IAAIY,WAAWA,UAAUC,WAAW,CAACV;IACtC,GACA;QAAEW,UAAU;IAAK;IAGlB,OAAOZ,OAAOa,cAAc,GAAGC,MAAM;AACtC,EAAE;AAEF;;;;;CAKC,GACD,OAAO,MAAMC,gBAAgB,CAACC;IAC7B,8BAA8B;IAC9B,MAAMV,MAAM,IAAIP,MAAM;IAEtB,aAAa;IACbkB,WAAWV,MAAM,GAAGD,IAAIC,MAAM;IAC9BU,WAAWT,QAAQ,GAAGF,IAAIC,MAAM,CAACC,QAAQ;IAEzCR,OAAOK,MAAM,CAAC;QACb,MAAMa,cAAclB,OAAOmB,gBAAgB,CAACH;QAC5ChB,OAAOoB,cAAc,CAACF;IACvB;IAEA,mCAAmC;IACnC,IAAId,OAAO;IACXJ,OAAOa,cAAc,GAAGQ,IAAI,CAAC;QAC5BjB,OAAOR,uBAAuBI;IAC/B;IAEA,OAAOI;AACR,EAAE"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { htmlToLexical } from './lexical';
|
|
2
|
+
describe('htmlToLexical', ()=>{
|
|
3
|
+
it('should convert an HTML string to a Lexical editor state', ()=>{
|
|
4
|
+
const html = '<p>Hello, world!</p>';
|
|
5
|
+
const editorState = htmlToLexical(html);
|
|
6
|
+
expect(editorState).toEqual({
|
|
7
|
+
nodes: [
|
|
8
|
+
{
|
|
9
|
+
type: 'paragraph',
|
|
10
|
+
children: [
|
|
11
|
+
{
|
|
12
|
+
text: 'Hello, world!'
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
}
|
|
16
|
+
]
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=lexical.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/util/lexical.test.ts"],"sourcesContent":["import { htmlToLexical } from './lexical';\n\ndescribe('htmlToLexical', () => {\n\tit('should convert an HTML string to a Lexical editor state', () => {\n\t\tconst html = '<p>Hello, world!</p>';\n\t\tconst editorState = htmlToLexical(html);\n\n\t\texpect(editorState).toEqual({\n\t\t\tnodes: [\n\t\t\t\t{\n\t\t\t\t\ttype: 'paragraph',\n\t\t\t\t\tchildren: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\ttext: 'Hello, world!',\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t],\n\t\t});\n\t});\n});\n"],"names":["htmlToLexical","describe","it","html","editorState","expect","toEqual","nodes","type","children","text"],"mappings":"AAAA,SAASA,aAAa,QAAQ,YAAY;AAE1CC,SAAS,iBAAiB;IACzBC,GAAG,2DAA2D;QAC7D,MAAMC,OAAO;QACb,MAAMC,cAAcJ,cAAcG;QAElCE,OAAOD,aAAaE,OAAO,CAAC;YAC3BC,OAAO;gBACN;oBACCC,MAAM;oBACNC,UAAU;wBACT;4BACCC,MAAM;wBACP;qBACA;gBACF;aACA;QACF;IACD;AACD"}
|