Skip to content

Commit

Permalink
Add repeat case to link snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-mp committed Nov 5, 2024
1 parent a8a35dd commit 818334f
Show file tree
Hide file tree
Showing 4 changed files with 116 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

0 comments on commit 818334f

Please sign in to comment.