Skip to content

Commit

Permalink
fix(blocks): support overlapping of inline styles (#6998)
Browse files Browse the repository at this point in the history
  • Loading branch information
Flrande committed May 10, 2024
1 parent 523d713 commit b5e411a
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 63 deletions.
20 changes: 10 additions & 10 deletions packages/blocks/src/_common/inline/presets/affine-inline-specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,6 @@ export const affineInlineSpecsWithoutReference: InlineSpecs<AffineTextAttributes
return html`<affine-text .delta=${delta}></affine-text>`;
},
},
{
name: 'link',
schema: z.string().optional().nullable().catch(undefined),
match: delta => {
return !!delta.attributes?.link;
},
renderer: delta => {
return html`<affine-link .delta=${delta}></affine-link>`;
},
},
{
name: 'background',
schema: z.string().optional().nullable().catch(undefined),
Expand Down Expand Up @@ -140,5 +130,15 @@ export function getAffineInlineSpecsWithReference(
},
embed: true,
},
{
name: 'link',
schema: z.string().optional().nullable().catch(undefined),
match: delta => {
return !!delta.attributes?.link;
},
renderer: delta => {
return html`<affine-link .delta=${delta}></affine-link>`;
},
},
];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ShadowlessElement } from '@blocksuite/block-std';
import { type DeltaInsert, ZERO_WIDTH_SPACE } from '@blocksuite/inline';
import { css, html } from 'lit';
import { html } from 'lit';
import { customElement, property } from 'lit/decorators.js';
import { type StyleInfo, styleMap } from 'lit/directives/style-map.js';

Expand Down Expand Up @@ -44,13 +44,6 @@ export function affineTextStyles(

@customElement('affine-text')
export class AffineText extends ShadowlessElement {
static override styles = css`
affine-text {
white-space: break-spaces;
word-break: break-word;
}
`;

@property({ type: Object })
delta: DeltaInsert<AffineTextAttributes> = {
insert: ZERO_WIDTH_SPACE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,9 @@ export class AffineLink extends ShadowlessElement {
}

static override styles = css`
affine-link > a {
white-space: nowrap;
word-break: break-word;
color: var(--affine-link-color);
fill: var(--affine-link-color);
text-decoration: none;
cursor: pointer;
}
affine-link > a:hover [data-v-text='true'] {
text-decoration: underline;
}
affine-link > a > v-text {
white-space: break-spaces;
}
`;

private _whenHover = new HoverController(this, ({ abortController }) => {
Expand Down Expand Up @@ -129,7 +116,12 @@ export class AffineLink extends ShadowlessElement {

override render() {
const style = this.delta.attributes
? affineTextStyles(this.delta.attributes)
? affineTextStyles(this.delta.attributes, {
color: 'var(--affine-link-color)',
fill: 'var(--affine-link-color)',
'text-decoration': 'none',
cursor: 'pointer',
})
: styleMap({});

return html`<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,10 @@ export class LinkPopup extends WithDisposable(LitElement) {
const link = normalizeUrl(linkInputValue);

if (this.type === 'create') {
this.inlineEditor.formatText(
this.targetInlineRange,
{
link: link,
reference: null,
},
{ mode: 'replace' }
);
this.inlineEditor.formatText(this.targetInlineRange, {
link: link,
reference: null,
});
this.inlineEditor.setInlineRange(this.targetInlineRange);
const textSelection = this.host.selection.find('text');
assertExists(textSelection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
margin-left: 4px;
border-bottom: 0.5px solid var(--affine-divider-color);
transition: border 0.2s ease-out;
white-space: break-spaces;
}
.affine-reference-title:hover {
border-bottom: 0.5px solid var(--affine-icon-color);
Expand Down
12 changes: 1 addition & 11 deletions packages/blocks/src/code-block/affine-code-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@ export class AffineCodeLine extends ShadowlessElement {
const { lang, highlighter } = this.highlightOptionsGetter();

if (!highlighter || !highlighter.getLoadedLanguages().includes(lang)) {
return html`<span
><v-text
.str=${this.delta.insert}
.styles=${{
'word-wrap': 'break-word',
'white-space': 'break-spaces',
}}
></v-text
></span>`;
return html`<span><v-text .str=${this.delta.insert}></v-text></span>`;
}

const mode = getThemeMode();
Expand All @@ -66,8 +58,6 @@ export class AffineCodeLine extends ShadowlessElement {
return html`<v-text
.str=${token.content}
.styles=${{
'word-wrap': 'break-word',
'white-space': 'break-spaces',
color: token.color,
}}
></v-text>`;
Expand Down
5 changes: 4 additions & 1 deletion packages/framework/inline/src/components/v-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export class VText extends LitElement {
@property({ attribute: false })
styles: StyleInfo = {
'word-break': 'break-word',
'white-space': 'pre-wrap',
// In most cases, break-spaces should be used.
// When replacing, pay attention to the differences in handling spaces for different keywords.
// https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
'white-space': 'break-spaces',
cursor: 'text',
};

Expand Down
2 changes: 0 additions & 2 deletions packages/framework/inline/src/utils/attribute-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ function inlineTextStyles(
}

return styleMap({
'word-wrap': 'break-word',
'white-space': 'break-spaces',
'font-weight': props.bold ? 'bold' : 'normal',
'font-style': props.italic ? 'italic' : 'normal',
'text-decoration': textDecorations.length > 0 ? textDecorations : 'none',
Expand Down
9 changes: 1 addition & 8 deletions packages/playground/examples/inline/test-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ function inlineTextStyles(
}

return styleMap({
'word-wrap': 'break-word',
'white-space': 'break-spaces',
'font-weight': props.bold ? 'bold' : 'normal',
'font-style': props.italic ? 'italic' : 'normal',
'text-decoration': textDecorations.length > 0 ? textDecorations : 'none',
Expand All @@ -73,10 +71,7 @@ const attributeRenderer: AttributeRenderer = (

const style = delta.attributes
? inlineTextStyles(delta.attributes)
: styleMap({
'white-space': 'break-spaces',
'word-wrap': 'break-word',
});
: styleMap({});

return html`<span style=${style}
><v-text .str=${delta.insert}></v-text
Expand Down Expand Up @@ -204,8 +199,6 @@ export class TestRichText extends ShadowlessElement {
.rich-text-container {
outline: none;
word-break: break-word;
white-space: break-spaces;
}
code {
Expand Down

0 comments on commit b5e411a

Please sign in to comment.