Skip to content

Commit

Permalink
Merge pull request #1477 from prismicio/dani/snippets
Browse files Browse the repository at this point in the history
feat: repeatable links code snippets
  • Loading branch information
dani-mp authored Nov 13, 2024
2 parents 5a49744 + 87ce5c7 commit 69439db
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 40 deletions.
39 changes: 29 additions & 10 deletions packages/adapter-next/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,38 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
}

case "Link": {
const repeat = data.model.config?.repeat ?? false;
const allowText = data.model.config?.allowText ?? false;

let codeText;
if (!repeat && !allowText) {
codeText = stripIndent`
<PrismicNextLink field={${dotPath(fieldPath)}}>Link</PrismicNextLink>
`;
} else if (!repeat && allowText) {
codeText = stripIndent`
<PrismicNextLink field={${dotPath(fieldPath)}} />
`;
} else if (repeat && !allowText) {
codeText = stripIndent`
{${dotPath(fieldPath)}.map((link, index) => (
<PrismicNextLink key={index} field={link}>Link</PrismicNextLink>
))}
`;
} else if (repeat && allowText) {
codeText = stripIndent`
{${dotPath(fieldPath)}.map((link, index) => (
<PrismicNextLink key={index} field={link} />
))}
`;
} else {
throw new Error("Invalid configuration.");
}

return {
label,
language: "tsx",
code: await format(
data.model.config?.allowText
? stripIndent`
<PrismicNextLink field={${dotPath(fieldPath)}} />
`
: stripIndent`
<PrismicNextLink field={${dotPath(fieldPath)}}>Link</PrismicNextLink>
`,
helpers,
),
code: await format(codeText, helpers),
};
}

Expand Down
39 changes: 29 additions & 10 deletions packages/adapter-nuxt/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,38 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
}

case "Link": {
const repeat = data.model.config?.repeat ?? false;
const allowText = data.model.config?.allowText ?? false;

let codeText;
if (!repeat && !allowText) {
codeText = stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}">Link</PrismicLink>
`;
} else if (!repeat && allowText) {
codeText = stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}" />
`;
} else if (repeat && !allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<PrismicLink :field="link">Link</PrismicLink>
</template>
`;
} else if (repeat && allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<PrismicLink :field="link" />
</template>
`;
} else {
throw new Error("Invalid configuration.");
}

return {
label,
language: "vue",
code: await format(
data.model.config?.allowText
? stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}" />
`
: stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}">Link</PrismicLink>
`,
helpers,
),
code: await format(codeText, helpers),
};
}

Expand Down
39 changes: 29 additions & 10 deletions packages/adapter-nuxt2/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,38 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
}

case "Link": {
const repeat = data.model.config?.repeat ?? false;
const allowText = data.model.config?.allowText ?? false;

let codeText;
if (!repeat && !allowText) {
codeText = stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}">Link</PrismicLink>
`;
} else if (!repeat && allowText) {
codeText = stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}" />
`;
} else if (repeat && !allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<PrismicLink :field="link">Link</PrismicLink>
</template>
`;
} else if (repeat && allowText) {
codeText = stripIndent`
<template v-for="(link, index) in ${dotPath(fieldPath)}" :key="index">
<PrismicLink :field="link" />
</template>
`;
} else {
throw new Error("Invalid configuration.");
}

return {
label,
language: "vue",
code: await format(
data.model.config?.allowText
? stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}" />
`
: stripIndent`
<PrismicLink :field="${dotPath(fieldPath)}">Link</PrismicLink>
`,
helpers,
),
code: await format(codeText, helpers),
};
}

Expand Down
39 changes: 29 additions & 10 deletions packages/adapter-sveltekit/src/hooks/snippet-read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,38 @@ export const snippetRead: SnippetReadHook<PluginOptions> = async (
}

case "Link": {
const repeat = data.model.config?.repeat ?? false;
const allowText = data.model.config?.allowText ?? false;

let codeText;
if (!repeat && !allowText) {
codeText = stripIndent`
<PrismicLink field={${dotPath(fieldPath)}}>Link</PrismicLink>
`;
} else if (!repeat && allowText) {
codeText = stripIndent`
<PrismicLink field={${dotPath(fieldPath)}} />
`;
} else if (repeat && !allowText) {
codeText = stripIndent`
{#each ${dotPath(fieldPath)} as link, index (index)}
<PrismicLink field={link}>Link</PrismicLink>
{/each}
`;
} else if (repeat && allowText) {
codeText = stripIndent`
{#each ${dotPath(fieldPath)} as link, index (index)}
<PrismicLink field={link} />
{/each}
`;
} else {
throw new Error("Invalid configuration.");
}

return {
label,
language: "svelte",
code: await format(
data.model.config?.allowText
? stripIndent`
<PrismicLink field={${dotPath(fieldPath)}} />
`
: stripIndent`
<PrismicLink field={${dotPath(fieldPath)}}>Link</PrismicLink>
`,
helpers,
),
code: await format(codeText, helpers),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Hint: React.FC<HintProps> = ({
if (item.value.type === "Link") {
if (item.value.config?.allowText ?? false)
snippetCacheKey.push("allowText");
if (item.value.config?.repeat ?? false) snippetCacheKey.push("repeat");
}

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
Expand Down

0 comments on commit 69439db

Please sign in to comment.