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

fix: Placeholder error handling for inserting CSS rules #1300

Merged
merged 1 commit into from
Dec 3, 2024
Merged
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
38 changes: 23 additions & 15 deletions packages/core/src/extensions/Placeholder/PlaceholderPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,30 @@ export class PlaceholderPlugin {
for (const [blockType, placeholder] of Object.entries(placeholders)) {
const mustBeFocused = blockType === "default";

styleSheet.insertRule(
`${getSelector(
blockType,
mustBeFocused
)}{ content: ${JSON.stringify(placeholder)}; }`
);

// For some reason, the placeholders which show when the block is focused
// take priority over ones which show depending on block type, so we need
// to make sure the block specific ones are also used when the block is
// focused.
if (!mustBeFocused) {
try {
styleSheet.insertRule(
`${getSelector(blockType, true)}{ content: ${JSON.stringify(
placeholder
)}; }`
`${getSelector(
blockType,
mustBeFocused
)} { content: ${JSON.stringify(placeholder)}; }`
);

// For some reason, the placeholders which show when the block is focused
// take priority over ones which show depending on block type, so we need
// to make sure the block specific ones are also used when the block is
// focused.
if (!mustBeFocused) {
styleSheet.insertRule(
`${getSelector(blockType, true)} { content: ${JSON.stringify(
placeholder
)}; }`
);
}
} catch (e) {
// eslint-disable-next-line no-console
console.warn(
`Failed to insert placeholder CSS rule - this is likely due to the browser not supporting certain CSS pseudo-element selectors (:has, :only-child:, or :before)`,
e
);
}
}
Expand Down
Loading