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

Feat/mark affinity #3147

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Feat/mark affinity #3147

wants to merge 4 commits into from

Conversation

felixfeng33
Copy link
Collaborator

@felixfeng33 felixfeng33 commented Apr 23, 2024

@12joan We have discussion at #2530 .Are these the contents you expressed?

Copy link

changeset-bot bot commented Apr 23, 2024

⚠️ No Changeset found

Latest commit: 841e8ab

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@felixfeng33 felixfeng33 marked this pull request as draft April 23, 2024 07:36
Copy link

vercel bot commented Apr 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plate ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 26, 2024 7:20am

Copy link
Collaborator

@12joan 12joan left a comment

Choose a reason for hiding this comment

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

Nice! I've left some comments.

apps/www/package.json Outdated Show resolved Hide resolved
packages/affinity-mark/src/createMarkAffinityPlugin.ts Outdated Show resolved Hide resolved
packages/affinity-mark/src/createMarkAffinityPlugin.ts Outdated Show resolved Hide resolved
packages/affinity-mark/src/getMarkBoundaryAffinity.ts Outdated Show resolved Hide resolved
packages/affinity-mark/CHANGELOG.md Outdated Show resolved Hide resolved
packages/affinity-mark/README.md Outdated Show resolved Hide resolved
packages/affinity-mark/package.json Outdated Show resolved Hide resolved
@felixfeng33
Copy link
Collaborator Author

felixfeng33 commented Apr 26, 2024

@12joan I have accept your suggestion. I intend to spilt this feature into three plugin.This is the createNaturalMarkAffinityPlugin one.
it exclude the mark which have padding-left or padding-right like MARK_CODE.
Due to limitations with Slate, we cannot set the cursor to the far left and within the code.and this mark have padding-left it will be strange.
It should create a new plugin to handle this case.Right now I have no ideal.
image

The third plugin will conflict with the first plugin, resulting in a cursor that moves in steps.

@felixfeng33 felixfeng33 requested a review from 12joan April 26, 2024 07:29
@12joan
Copy link
Collaborator

12joan commented Apr 26, 2024

it exclude the mark which have padding-left or padding-right like MARK_CODE.
Due to limitations with Slate, we cannot set the cursor to the far left and within the code.and this mark have padding-left it will be strange.
It should create a new plugin to handle this case.Right now I have no ideal.
image

Is this the bug where setting the affinity to the left while at the start of an inline code mark creates a new, empty inline code mark to the left of it? If it's possible to fix that (which I'm not 100% sure it is), that would have to be fixed in slate-react.

In my app, I've addressed this by switching to an inline code style without any padding.
image

@felixfeng33
Copy link
Collaborator Author

it exclude the mark which have padding-left or padding-right like MARK_CODE.
Due to limitations with Slate, we cannot set the cursor to the far left and within the code.and this mark have padding-left it will be strange.
It should create a new plugin to handle this case.Right now I have no ideal.
image

Is this the bug where setting the affinity to the left while at the start of an inline code mark creates a new, empty inline code mark to the left of it? If it's possible to fix that (which I'm not 100% sure it is), that would have to be fixed in slate-react.

In my app, I've addressed this by switching to an inline code style without any padding. image

yeah,it have to be fixed in slate-react I think it will be not easy.

Copy link
Collaborator

@12joan 12joan left a comment

Choose a reason for hiding this comment

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

Thanks for making those changes!

I've left a few more comments, although I'm sure many of them are things you had planned to address anyway.

@@ -82,6 +82,7 @@
"@udecode/plate-line-height": "workspace:^",
"@udecode/plate-link": "workspace:^",
"@udecode/plate-list": "workspace:^",
"@udecode/plate-marks-affinity": "workspace:^",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"@udecode/plate-marks-affinity": "workspace:^",
"@udecode/plate-mark-affinity": "workspace:^",

@@ -0,0 +1,15 @@
# Plate marks affinity plugins
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs proofreading and editing, but this can wait until we're closer to merging.

Comment on lines +2 to +4
"name": "@udecode/plate-marks-affinity",
"version": "31.0.0",
"description": "marks affinity plugin for Plate",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"name": "@udecode/plate-marks-affinity",
"version": "31.0.0",
"description": "marks affinity plugin for Plate",
"name": "@udecode/plate-mark-affinity",
"version": "31.0.0",
"description": "Mark affinity plugin for Plate",

import { NodeEntry } from 'slate';

export interface MarkAffinityPlugin {
validMarks?: string[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we want to support using a function here to support more advanced use cases?

* boundary, then the affinity should be forward. If the deletion removes
* a character from the left mark, then the affinity should be backward.
*/
editor.deleteBackward = (unit) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should also be controlled by the plugin option

Copy link
Collaborator

@12joan 12joan Apr 26, 2024

Choose a reason for hiding this comment

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

You'll probably want to refactor out the query logic into a helper that can be reused in all the places that need to determine if a mark affinity behaviour is active.

Copy link
Collaborator

Choose a reason for hiding this comment

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

afterMarkBoundary is the mark boundary that should be considered for the query

Comment on lines +57 to +59
/**
* check the validMarks exclude the marks which have padding like `MARK_CODE`
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is the consumer's responsibility, since many apps won't have padding on their code marks.

Comment on lines +68 to +73
if (!validMarks) return move(options);
if (marks.length > 0) {
for (const mark of marks) {
if (!validMarks.includes(mark)) return move(options);
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The text nodes either side of the mark boundary should be compared against the list of allowed marks, such that the plugin is active if at least one allowed mark is present either side of the boundary

Ideally, to support more advanced use cases, we should also let the consumer specify a function that takes a mark boundary and returns a boolean.

This logic might be useful as a starting point:

typeof query === 'function'
  ? query(markBoundary)
  : markBoundary.some(
    (leafEntry) => leafEntry && Object.keys(getNodeProps(leafEntry)).some(
      (mark) => query.includes(mark)
    )
  )

Comment on lines +75 to +94
const beforeMarkBoundary = getMarkBoundary(editor);

/**
* If the cursor is at the start or end of a list of text nodes
* then moving outside the mark should set the
* affinity accordingly.
*/
if (
beforeMarkBoundary &&
beforeMarkBoundary[reverse ? 0 : 1] === null &&
getMarkBoundaryAffinity(editor, beforeMarkBoundary) ===
(reverse ? 'forward' : 'backward')
) {
setMarkBoundaryAffinity(
editor,
beforeMarkBoundary,
reverse ? 'backward' : 'forward'
);
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This behaviour should be moved into a separate plugin with its own list of allowed marks, since we probably don't want it enabled for marks like bold.

recording.mp4

In my app, I've enabled this only for code marks, since those don't have a keyboard shortcut and so are more frustrating to disable at the end of a line.

Since Plate's default code marks have padding, perhaps the default playground should only enable this behaviour for code marks at the end of a line, not at the start? If we go with that solution, there would need to be an option to re-enable it at the start of the line too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

beforeMarkBoundary is the mark boundary that should be considered when performing the query for this behaviour.


move(options);

const afterMarkBoundary = getMarkBoundary(editor);
Copy link
Collaborator

@12joan 12joan Apr 26, 2024

Choose a reason for hiding this comment

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

afterMarkBoundary is the mark boundary that should be considered when performing the query for the natural mark affinity behaviour below.

@felixfeng33
Copy link
Collaborator Author

Thank your valuable advice. @12joan

@admsev
Copy link

admsev commented May 26, 2024

@gooroodev are you able to review this?

@gooroodev
Copy link

Appreciate the heads-up, @admsev!

Summary of Changes

  1. New Package Addition:

    • Added a new package @udecode/plate-marks-affinity to manage mark affinity in the editor.
  2. Dependency Updates:

    • Updated apps/www/package.json to include the new package as a dependency.
  3. Playground Demo:

    • Modified apps/www/src/registry/default/example/playground-demo.tsx to integrate the new mark affinity plugin.
    • Added new constants for various marks such as MARK_BOLD, MARK_ITALIC, etc., to the imports.
    • Integrated createMarkAffinityPlugin with valid marks in the usePlaygroundPlugins function.
  4. New Package Files:

    • Created new files in the packages/mark-affinity directory:
      • .npmignore
      • README.md
      • package.json
      • src/createMarkAffinityPlugin.ts
      • src/index.ts
      • src/queries/getMarkBoundary.ts
      • src/queries/getMarkBoundaryAffinity.ts
      • src/queries/index.ts
      • src/transforms/index.ts
      • src/transforms/setMarkBoundaryAffinity.ts
      • src/types.ts
      • src/withMarkAffinity.ts
      • tsconfig.json
  5. Utility Updates:

    • Modified packages/utils/src/environment.ts to include a new constant IS_FIREFOX.

Issues, Bugs, or Typos

  1. Typo in README:

    • In packages/mark-affinity/README.md, the word "futures" should be corrected to "features".
  2. Comment Issue:

    • In withMarkAffinity.ts, there's a TODO comment that seems incomplete: //TODO: note the comment_x. It should be either completed or removed if not needed.
  3. Unnecessary Null Checks:

    • In getMarkBoundary.ts, the return type of getNodeEntry is already nullable, so the explicit ?? null checks might be redundant.

Code Improvements

  1. Typo Correction in README:

    - This package implements the following futures:
    + This package implements the following features:
  2. Complete or Remove TODO Comment:

    - //TODO: note the comment_x
  3. Simplify Null Checks in getMarkBoundary.ts:

    - const adjacentNode = getNodeEntry(editor, adjacentPath) ?? null;
    + const adjacentNode = getNodeEntry(editor, adjacentPath);
    
    - const adjacentLeafEntry =
    -   adjacentNode && isText(adjacentNode[0])
    -     ? (adjacentNode as NodeEntry<TText>)
    -     : null;
    + const adjacentLeafEntry = adjacentNode && isText(adjacentNode[0])
    +   ? (adjacentNode as NodeEntry<TText>)
    +   : null;

General Review of Code Quality and Style

  1. Code Consistency:

    • The code is consistent in terms of style and follows typical TypeScript and React best practices.
    • Proper use of TypeScript interfaces and types enhances code readability and maintainability.
  2. Documentation:

    • The newly added README.md provides a brief overview and a link to further documentation, which is good for new users of the package.
  3. Modular Design:

    • The new functionality is well encapsulated within its package, adhering to the modular design principles.
  4. Code Comments:

    • The code is well-commented, explaining the purpose of various functions and the logic behind certain decisions.
  5. Error Handling:

    • The code generally checks for null or undefined values, ensuring robustness.

Overall, the changes are well-structured and integrate seamlessly with the existing codebase. The new functionality for managing mark affinity is clearly defined and implemented in a modular fashion.

Yours, Gooroo.dev. Please add a reaction or reply with your thoughts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants