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(blocks): unexpected newline appears while pasting multiply-paragr… #8154

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/blocks/src/_common/adapters/plain-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export class PlainTextAdapter extends BaseAdapter<PlainText> {
return null;
}
payload.file = payload.file.replaceAll('\r', '');
payload.file = payload.file.replace(/\n$/, '');
Copy link
Contributor

@L-Sun L-Sun Sep 5, 2024

Choose a reason for hiding this comment

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

I think the reason is in the exporter side, not here. @fourdim cc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, i think so too , here is the export side in /packages/blocks/src/_common/adapters/plain-text.ts

  private async _traverseSnapshot(
    snapshot: BlockSnapshot
  ): Promise<{ mixtext: string }> {
    ...
    walker.setEnter(o => {
      const text = (o.node.props.text ?? { delta: [] }) as {
        delta: DeltaInsert[];
      };
      switch (o.node.flavour) {
        ...
        case 'affine:paragraph': {
          buffer += text.delta.map(delta => delta.insert).join('');
          buffer += '\n';
          break;
        }
 ...

but i think change here will affect a lot , so i only change the adapter

Copy link
Contributor

Choose a reason for hiding this comment

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

No, changing this adapter will affect a lot.

It's not about adding an extra newline at the end of a file, it's about not removing the newline that should be there.

The C standard specifies that a C file should end with a newline (C11, 5.1.1.2, 2.) and that a last line without a newline yields undefined behavior (C11, J.2, 2nd item).

https://unix.stackexchange.com/questions/18743/whats-the-point-in-adding-a-new-line-to-the-end-of-a-file

const contentSlice = {
type: 'block',
id: nanoid(),
Expand Down