Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: repeatable links code snippets #1477

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
`;
Comment on lines +73 to +84
Copy link
Contributor

@jomifepe jomifepe Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why, but I wasn't able to see the correct snippet with repeat, does it work correctly for you?

Kapture.2024-11-12.at.10.22.41.mp4

I can see it for Next and Sveltekit.

Screenshot 2024-11-12 at 10 13 14

Screenshot 2024-11-12 at 10 19 17

} 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
Loading