-
-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: goodbye legacy rich text operation (#7988)
- Loading branch information
1 parent
1b2b60c
commit b8c2ef5
Showing
11 changed files
with
236 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
packages/blocks/src/_common/components/rich-text/keymap/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './container.js'; | ||
export * from './legacy.js'; |
28 changes: 0 additions & 28 deletions
28
packages/blocks/src/_common/components/rich-text/keymap/legacy.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/blocks/src/paragraph-block/commands/add-paragraph.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Command } from '@blocksuite/block-std'; | ||
|
||
import { focusTextModel } from '@blocksuite/affine-components/rich-text'; | ||
|
||
/** | ||
* Add a paragraph next to the current block. | ||
*/ | ||
export const addParagraphCommand: Command< | ||
never, | ||
'paragraphConvertedId', | ||
{ | ||
blockId?: string; | ||
} | ||
> = (ctx, next) => { | ||
const { std } = ctx; | ||
const { doc, selection } = std; | ||
doc.captureSync(); | ||
|
||
let blockId = ctx.blockId; | ||
if (!blockId) { | ||
const text = selection.find('text'); | ||
blockId = text?.blockId; | ||
} | ||
if (!blockId) return; | ||
|
||
const model = doc.getBlock(blockId)?.model; | ||
if (!model) return; | ||
|
||
let id: string; | ||
if (model.children.length > 0) { | ||
// before: | ||
// aaa| | ||
// bbb | ||
// | ||
// after: | ||
// aaa | ||
// | | ||
// bbb | ||
id = doc.addBlock('affine:paragraph', {}, model, 0); | ||
} else { | ||
const parent = doc.getParent(model); | ||
if (!parent) return; | ||
const index = parent.children.indexOf(model); | ||
if (index < 0) return; | ||
// before: | ||
// aaa| | ||
// | ||
// after: | ||
// aaa | ||
// | | ||
id = doc.addBlock('affine:paragraph', {}, parent, index + 1); | ||
} | ||
|
||
focusTextModel(std, id); | ||
return next({ paragraphConvertedId: id }); | ||
}; | ||
|
||
declare global { | ||
namespace BlockSuite { | ||
interface Commands { | ||
addParagraph: typeof addParagraphCommand; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import type { BlockCommands } from '@blocksuite/block-std'; | ||
|
||
import { addParagraphCommand } from './add-paragraph.js'; | ||
import { appendParagraphCommand } from './append-paragraph.js'; | ||
import { splitParagraphCommand } from './split-paragraph.js'; | ||
|
||
export const commands: BlockCommands = { | ||
appendParagraph: appendParagraphCommand, | ||
splitParagraph: splitParagraphCommand, | ||
addParagraph: addParagraphCommand, | ||
}; | ||
|
||
declare global { | ||
namespace BlockSuite { | ||
interface CommandContext { | ||
paragraphConvertedId?: string; | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/blocks/src/paragraph-block/commands/split-paragraph.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import type { Command } from '@blocksuite/block-std'; | ||
|
||
import { | ||
focusTextModel, | ||
getInlineEditorByModel, | ||
} from '@blocksuite/affine-components/rich-text'; | ||
import { matchFlavours } from '@blocksuite/affine-shared/utils'; | ||
|
||
export const splitParagraphCommand: Command< | ||
never, | ||
'paragraphConvertedId', | ||
{ | ||
blockId?: string; | ||
} | ||
> = (ctx, next) => { | ||
const { std } = ctx; | ||
const { doc, host, selection } = std; | ||
let blockId = ctx.blockId; | ||
if (!blockId) { | ||
const text = selection.find('text'); | ||
blockId = text?.blockId; | ||
} | ||
if (!blockId) return; | ||
|
||
const model = doc.getBlock(blockId)?.model; | ||
if (!model || !matchFlavours(model, ['affine:paragraph'])) return; | ||
|
||
const inlineEditor = getInlineEditorByModel(host, model); | ||
const range = inlineEditor?.getInlineRange(); | ||
if (!range) return; | ||
|
||
const splitIndex = range.index; | ||
const splitLength = range.length; | ||
// On press enter, it may convert symbols from yjs ContentString | ||
// to yjs ContentFormat. Once it happens, the converted symbol will | ||
// be deleted and not counted as model.text.yText.length. | ||
// Example: "`a`[enter]" -> yText[<ContentFormat: Code>, "a", <ContentFormat: Code>] | ||
// In this case, we should not split the block. | ||
if (model.text.yText.length < splitIndex + splitLength) return; | ||
|
||
if (model.children.length > 0 && splitIndex > 0) { | ||
doc.captureSync(); | ||
const right = model.text.split(splitIndex, splitLength); | ||
const id = doc.addBlock( | ||
model.flavour as BlockSuite.Flavour, | ||
{ | ||
text: right, | ||
type: model.type, | ||
}, | ||
model, | ||
0 | ||
); | ||
focusTextModel(std, id); | ||
return next({ paragraphConvertedId: id }); | ||
} | ||
|
||
const parent = doc.getParent(model); | ||
if (!parent) return; | ||
const index = parent.children.indexOf(model); | ||
if (index < 0) return; | ||
doc.captureSync(); | ||
const right = model.text.split(splitIndex, splitLength); | ||
const id = doc.addBlock( | ||
model.flavour, | ||
{ | ||
text: right, | ||
type: model.type, | ||
}, | ||
parent, | ||
index + 1 | ||
); | ||
const newModel = doc.getBlock(id)?.model; | ||
if (newModel) { | ||
doc.moveBlocks(model.children, newModel); | ||
} else { | ||
console.error('Failed to find the new model split from the paragraph'); | ||
} | ||
focusTextModel(std, id); | ||
return next({ paragraphConvertedId: id }); | ||
}; | ||
|
||
declare global { | ||
namespace BlockSuite { | ||
interface Commands { | ||
splitParagraph: typeof splitParagraphCommand; | ||
} | ||
} | ||
} |
Oops, something went wrong.