refactor(editor): remove paragraph service (#11003)
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
export * from './adapters/index.js';
|
export * from './adapters/index.js';
|
||||||
export * from './commands';
|
export * from './commands';
|
||||||
export * from './paragraph-block.js';
|
export * from './paragraph-block.js';
|
||||||
export * from './paragraph-service.js';
|
export * from './paragraph-block-config.js';
|
||||||
export * from './paragraph-spec.js';
|
export * from './paragraph-spec.js';
|
||||||
export * from './turbo/paragraph-layout-provider.js';
|
export * from './turbo/paragraph-layout-provider.js';
|
||||||
export * from './turbo/paragraph-painter.worker.js';
|
export * from './turbo/paragraph-painter.worker.js';
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
import type { ParagraphBlockModel } from '@blocksuite/affine-model';
|
||||||
|
import { ConfigExtensionFactory } from '@blocksuite/block-std';
|
||||||
|
|
||||||
|
export interface ParagraphBlockConfig {
|
||||||
|
getPlaceholder: (model: ParagraphBlockModel) => string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ParagraphBlockConfigExtension =
|
||||||
|
ConfigExtensionFactory<ParagraphBlockConfig>('affine:paragraph');
|
||||||
@@ -25,13 +25,10 @@ import { classMap } from 'lit/directives/class-map.js';
|
|||||||
import { styleMap } from 'lit/directives/style-map.js';
|
import { styleMap } from 'lit/directives/style-map.js';
|
||||||
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
|
||||||
|
|
||||||
import type { ParagraphBlockService } from './paragraph-service.js';
|
import { ParagraphBlockConfigExtension } from './paragraph-block-config.js';
|
||||||
import { paragraphBlockStyles } from './styles.js';
|
import { paragraphBlockStyles } from './styles.js';
|
||||||
|
|
||||||
export class ParagraphBlockComponent extends CaptionedBlockComponent<
|
export class ParagraphBlockComponent extends CaptionedBlockComponent<ParagraphBlockModel> {
|
||||||
ParagraphBlockModel,
|
|
||||||
ParagraphBlockService
|
|
||||||
> {
|
|
||||||
static override styles = paragraphBlockStyles;
|
static override styles = paragraphBlockStyles;
|
||||||
|
|
||||||
focused$ = computed(() => {
|
focused$ = computed(() => {
|
||||||
@@ -59,6 +56,12 @@ export class ParagraphBlockComponent extends CaptionedBlockComponent<
|
|||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private get _placeholder() {
|
||||||
|
return this.std
|
||||||
|
.get(ParagraphBlockConfigExtension.identifier)
|
||||||
|
?.getPlaceholder(this.model);
|
||||||
|
}
|
||||||
|
|
||||||
get attributeRenderer() {
|
get attributeRenderer() {
|
||||||
return this.inlineManager.getRenderer();
|
return this.inlineManager.getRenderer();
|
||||||
}
|
}
|
||||||
@@ -309,7 +312,7 @@ export class ParagraphBlockComponent extends CaptionedBlockComponent<
|
|||||||
visible: this._displayPlaceholder.value,
|
visible: this._displayPlaceholder.value,
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
${this.service.placeholderGenerator(this.model)}
|
${this._placeholder}
|
||||||
</div>
|
</div>
|
||||||
`}
|
`}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
import {
|
|
||||||
type ParagraphBlockModel,
|
|
||||||
ParagraphBlockSchema,
|
|
||||||
} from '@blocksuite/affine-model';
|
|
||||||
import { BlockService } from '@blocksuite/block-std';
|
|
||||||
|
|
||||||
export class ParagraphBlockService extends BlockService {
|
|
||||||
static override readonly flavour = ParagraphBlockSchema.model.flavour;
|
|
||||||
|
|
||||||
placeholderGenerator: (model: ParagraphBlockModel) => string = model => {
|
|
||||||
if (model.props.type === 'text') {
|
|
||||||
return "Type '/' for commands";
|
|
||||||
}
|
|
||||||
|
|
||||||
const placeholders = {
|
|
||||||
h1: 'Heading 1',
|
|
||||||
h2: 'Heading 2',
|
|
||||||
h3: 'Heading 3',
|
|
||||||
h4: 'Heading 4',
|
|
||||||
h5: 'Heading 5',
|
|
||||||
h6: 'Heading 6',
|
|
||||||
quote: '',
|
|
||||||
};
|
|
||||||
return placeholders[model.props.type];
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -3,17 +3,32 @@ import type { ExtensionType } from '@blocksuite/store';
|
|||||||
import { literal } from 'lit/static-html.js';
|
import { literal } from 'lit/static-html.js';
|
||||||
|
|
||||||
import { ParagraphBlockAdapterExtensions } from './adapters/extension.js';
|
import { ParagraphBlockAdapterExtensions } from './adapters/extension.js';
|
||||||
|
import { ParagraphBlockConfigExtension } from './paragraph-block-config.js';
|
||||||
import {
|
import {
|
||||||
ParagraphKeymapExtension,
|
ParagraphKeymapExtension,
|
||||||
ParagraphTextKeymapExtension,
|
ParagraphTextKeymapExtension,
|
||||||
} from './paragraph-keymap.js';
|
} from './paragraph-keymap.js';
|
||||||
import { ParagraphBlockService } from './paragraph-service.js';
|
|
||||||
|
const placeholders = {
|
||||||
|
text: "Type '/' for commands",
|
||||||
|
h1: 'Heading 1',
|
||||||
|
h2: 'Heading 2',
|
||||||
|
h3: 'Heading 3',
|
||||||
|
h4: 'Heading 4',
|
||||||
|
h5: 'Heading 5',
|
||||||
|
h6: 'Heading 6',
|
||||||
|
quote: '',
|
||||||
|
};
|
||||||
|
|
||||||
export const ParagraphBlockSpec: ExtensionType[] = [
|
export const ParagraphBlockSpec: ExtensionType[] = [
|
||||||
FlavourExtension('affine:paragraph'),
|
FlavourExtension('affine:paragraph'),
|
||||||
ParagraphBlockService,
|
|
||||||
BlockViewExtension('affine:paragraph', literal`affine-paragraph`),
|
BlockViewExtension('affine:paragraph', literal`affine-paragraph`),
|
||||||
ParagraphTextKeymapExtension,
|
ParagraphTextKeymapExtension,
|
||||||
ParagraphKeymapExtension,
|
ParagraphKeymapExtension,
|
||||||
ParagraphBlockAdapterExtensions,
|
ParagraphBlockAdapterExtensions,
|
||||||
|
ParagraphBlockConfigExtension({
|
||||||
|
getPlaceholder: model => {
|
||||||
|
return placeholders[model.props.type];
|
||||||
|
},
|
||||||
|
}),
|
||||||
].flat();
|
].flat();
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ export function ConfigExtensionFactory<Config extends Record<string, any>>(
|
|||||||
const identifier = ConfigIdentifier(flavor) as ServiceIdentifier<Config>;
|
const identifier = ConfigIdentifier(flavor) as ServiceIdentifier<Config>;
|
||||||
const extensionFactory = (config: Config): ExtensionType => ({
|
const extensionFactory = (config: Config): ExtensionType => ({
|
||||||
setup: di => {
|
setup: di => {
|
||||||
di.addImpl(ConfigIdentifier(flavor), () => config);
|
di.override(ConfigIdentifier(flavor), () => config);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
extensionFactory.identifier = identifier;
|
extensionFactory.identifier = identifier;
|
||||||
|
|||||||
@@ -1,22 +1,15 @@
|
|||||||
import { LifeCycleWatcher } from '@blocksuite/affine/block-std';
|
|
||||||
import {
|
import {
|
||||||
ParagraphBlockService,
|
ParagraphBlockConfigExtension,
|
||||||
ParagraphBlockSpec,
|
ParagraphBlockSpec,
|
||||||
} from '@blocksuite/affine/blocks/paragraph';
|
} from '@blocksuite/affine/blocks/paragraph';
|
||||||
import type { ExtensionType } from '@blocksuite/affine/store';
|
import type { ExtensionType } from '@blocksuite/affine/store';
|
||||||
|
|
||||||
class AIParagraphBlockWatcher extends LifeCycleWatcher {
|
export const AIParagraphBlockSpec: ExtensionType[] = [
|
||||||
static override key = 'ai-paragraph-block-watcher';
|
...ParagraphBlockSpec,
|
||||||
|
ParagraphBlockConfigExtension({
|
||||||
override mounted() {
|
getPlaceholder: model => {
|
||||||
super.mounted();
|
|
||||||
const service = this.std.get(ParagraphBlockService);
|
|
||||||
service.placeholderGenerator = model => {
|
|
||||||
if (model.props.type === 'text') {
|
|
||||||
return "Type '/' for commands, 'space' for AI";
|
|
||||||
}
|
|
||||||
|
|
||||||
const placeholders = {
|
const placeholders = {
|
||||||
|
text: "Type '/' for commands, 'space' for AI",
|
||||||
h1: 'Heading 1',
|
h1: 'Heading 1',
|
||||||
h2: 'Heading 2',
|
h2: 'Heading 2',
|
||||||
h3: 'Heading 3',
|
h3: 'Heading 3',
|
||||||
@@ -26,11 +19,6 @@ class AIParagraphBlockWatcher extends LifeCycleWatcher {
|
|||||||
quote: '',
|
quote: '',
|
||||||
};
|
};
|
||||||
return placeholders[model.props.type];
|
return placeholders[model.props.type];
|
||||||
};
|
},
|
||||||
}
|
}),
|
||||||
}
|
|
||||||
|
|
||||||
export const AIParagraphBlockSpec: ExtensionType[] = [
|
|
||||||
...ParagraphBlockSpec,
|
|
||||||
AIParagraphBlockWatcher,
|
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import {
|
|||||||
import type { CodeBlockConfig } from '@blocksuite/affine/blocks/code';
|
import type { CodeBlockConfig } from '@blocksuite/affine/blocks/code';
|
||||||
import { codeToolbarWidget } from '@blocksuite/affine/blocks/code';
|
import { codeToolbarWidget } from '@blocksuite/affine/blocks/code';
|
||||||
import { imageToolbarWidget } from '@blocksuite/affine/blocks/image';
|
import { imageToolbarWidget } from '@blocksuite/affine/blocks/image';
|
||||||
import { ParagraphBlockService } from '@blocksuite/affine/blocks/paragraph';
|
import { ParagraphBlockConfigExtension } from '@blocksuite/affine/blocks/paragraph';
|
||||||
import { surfaceRefToolbarWidget } from '@blocksuite/affine/blocks/surface-ref';
|
import { surfaceRefToolbarWidget } from '@blocksuite/affine/blocks/surface-ref';
|
||||||
import type {
|
import type {
|
||||||
Container,
|
Container,
|
||||||
@@ -68,14 +68,10 @@ class MobileSpecsPatches extends LifeCycleWatcher {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override mounted() {
|
const mobileParagraphConfig = ParagraphBlockConfigExtension({
|
||||||
// remove slash placeholder for mobile: `type / ...`
|
getPlaceholder: model => {
|
||||||
{
|
|
||||||
const paragraphService = this.std.get(ParagraphBlockService);
|
|
||||||
if (!paragraphService) return;
|
|
||||||
|
|
||||||
paragraphService.placeholderGenerator = model => {
|
|
||||||
const placeholders = {
|
const placeholders = {
|
||||||
text: '',
|
text: '',
|
||||||
h1: 'Heading 1',
|
h1: 'Heading 1',
|
||||||
@@ -87,10 +83,8 @@ class MobileSpecsPatches extends LifeCycleWatcher {
|
|||||||
quote: '',
|
quote: '',
|
||||||
};
|
};
|
||||||
return placeholders[model.props.type];
|
return placeholders[model.props.type];
|
||||||
};
|
},
|
||||||
}
|
});
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function KeyboardToolbarExtension(framework: FrameworkProvider): ExtensionType {
|
function KeyboardToolbarExtension(framework: FrameworkProvider): ExtensionType {
|
||||||
const affineVirtualKeyboardProvider = framework.get(VirtualKeyboardProvider);
|
const affineVirtualKeyboardProvider = framework.get(VirtualKeyboardProvider);
|
||||||
@@ -171,5 +165,9 @@ export function enableMobileExtension(
|
|||||||
specBuilder.omit(surfaceRefToolbarWidget);
|
specBuilder.omit(surfaceRefToolbarWidget);
|
||||||
specBuilder.omit(toolbarWidget);
|
specBuilder.omit(toolbarWidget);
|
||||||
specBuilder.omit(SlashMenuExtension);
|
specBuilder.omit(SlashMenuExtension);
|
||||||
specBuilder.extend([MobileSpecsPatches, KeyboardToolbarExtension(framework)]);
|
specBuilder.extend([
|
||||||
|
MobileSpecsPatches,
|
||||||
|
KeyboardToolbarExtension(framework),
|
||||||
|
mobileParagraphConfig,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user