-
Notifications
You must be signed in to change notification settings - Fork 841
Feat/mark affinity #3147
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
Closed
Closed
Feat/mark affinity #3147
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,3 @@ | ||
__tests__ | ||
__test-utils__ | ||
__mocks__ |
This file contains hidden or 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,15 @@ | ||
# Plate marks affinity plugins | ||
felixfeng33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This package implements the following futures: | ||
For example, if the editor has "boldn|ormal" and the user presses the left arrow key or | ||
backspace and then types, the resulting text will not be bold. If the editor has "bol|dnormal" | ||
and the user presses the right arrow key and types, the resulting text will be bold. | ||
|
||
## Documentation | ||
|
||
Check out | ||
[Basic Marks](https://platejs.org/docs/basic-marks-affinity). | ||
|
||
## License | ||
|
||
[MIT](../../LICENSE) |
This file contains hidden or 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,63 @@ | ||
{ | ||
"name": "@udecode/plate-marks-affinity", | ||
"version": "31.0.0", | ||
"description": "marks affinity plugin for Plate", | ||
felixfeng33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"license": "MIT", | ||
"homepage": "https://platejs.org", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/udecode/plate.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/udecode/plate/issues" | ||
}, | ||
"sideEffects": false, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist/**/*" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.mjs", | ||
"module": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"scripts": { | ||
"build": "yarn p:build", | ||
"build:watch": "yarn p:build:watch", | ||
"brl": "yarn p:brl", | ||
"clean": "yarn p:clean", | ||
"lint": "yarn p:lint", | ||
"lint:fix": "yarn p:lint:fix", | ||
"test": "yarn p:test", | ||
"test:watch": "yarn p:test:watch", | ||
"typecheck": "yarn p:typecheck" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
"@udecode/plate-common": "workspace:^" | ||
}, | ||
"peerDependencies": { | ||
"@udecode/plate-common": ">=31.3.2", | ||
"react": ">=16.8.0", | ||
"react-dom": ">=16.8.0", | ||
"slate": ">=0.94.0", | ||
"slate-history": ">=0.93.0", | ||
"slate-hyperscript": ">=0.66.0", | ||
"slate-react": ">=0.99.0" | ||
}, | ||
"keywords": [ | ||
"plate", | ||
"plugin", | ||
"slate" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
This file contains hidden or 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,13 @@ | ||
import { createPluginFactory } from '@udecode/plate-common'; | ||
|
||
import { MarkAffinityPlugin } from './types'; | ||
import { withMarkAffinity } from './withMarkAffinity'; | ||
|
||
export const KEY_MARK_AFFINITY = 'mark-affinity'; | ||
|
||
export const createMarkAffinityPlugin = createPluginFactory<MarkAffinityPlugin>( | ||
{ | ||
key: KEY_MARK_AFFINITY, | ||
withOverrides: withMarkAffinity, | ||
} | ||
); |
This file contains hidden or 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,9 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './createMarkAffinityPlugin'; | ||
export * from './types'; | ||
export * from './withMarkAffinity'; | ||
export * from './queries/index'; | ||
export * from './transforms/index'; |
This file contains hidden or 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,53 @@ | ||
import { | ||
getNodeEntry, | ||
getRange, | ||
isEndPoint, | ||
isSelectionExpanded, | ||
isStartPoint, | ||
isText, | ||
PlateEditor, | ||
TText, | ||
} from '@udecode/plate-common'; | ||
import { NodeEntry, Path } from 'slate'; | ||
|
||
import { MarkBoundary } from '../types'; | ||
|
||
export const getMarkBoundary = (editor: PlateEditor): MarkBoundary | null => { | ||
const { selection } = editor; | ||
if (!selection || isSelectionExpanded(editor)) return null; | ||
const point = selection.anchor; | ||
|
||
const selectedLeafRange = getRange(editor, point.path); | ||
|
||
const direction = (() => { | ||
if (isStartPoint(editor, point, selectedLeafRange)) { | ||
return 'backward'; | ||
} | ||
|
||
if (isEndPoint(editor, point, selectedLeafRange)) { | ||
return 'forward'; | ||
} | ||
|
||
return null; | ||
})(); | ||
|
||
if (!direction) return null; | ||
|
||
const currentLeafEntry = getNodeEntry<TText>(editor, point.path)!; | ||
|
||
if (direction === 'backward' && point.path.at(-1) === 0) | ||
return [null, currentLeafEntry]; | ||
|
||
const adjacentPathFn = direction === 'forward' ? Path.next : Path.previous; | ||
const adjacentPath = adjacentPathFn(point.path); | ||
const adjacentNode = getNodeEntry(editor, adjacentPath) ?? null; | ||
|
||
const adjacentLeafEntry = | ||
adjacentNode && isText(adjacentNode[0]) | ||
? (adjacentNode as NodeEntry<TText>) | ||
: null; | ||
|
||
return direction === 'forward' | ||
? [currentLeafEntry, adjacentLeafEntry] | ||
: [adjacentLeafEntry, currentLeafEntry]; | ||
}; |
54 changes: 54 additions & 0 deletions
54
packages/mark-affinity/src/queries/getMarkBoundaryAffinity.ts
This file contains hidden or 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,54 @@ | ||
import { | ||
getNodeProps, | ||
IS_FIREFOX, | ||
PlateEditor, | ||
TText, | ||
} from '@udecode/plate-common'; | ||
import isEqual from 'lodash/isEqual'; | ||
|
||
import { MarkBoundary } from '../types'; | ||
|
||
export const getMarkBoundaryAffinity = ( | ||
editor: PlateEditor, | ||
markBoundary: MarkBoundary | ||
): 'forward' | 'backward' | 'new-mark' => { | ||
const { marks, selection } = editor; | ||
if (!selection) return 'new-mark'; | ||
const marksMatchLeaf = (leaf: TText) => | ||
marks && isEqual(getNodeProps(leaf), marks); | ||
const [backwardLeafEntry, forwardLeafEntry] = markBoundary; | ||
|
||
if (!backwardLeafEntry || !forwardLeafEntry) { | ||
const leafEntry = backwardLeafEntry || forwardLeafEntry; | ||
const affinityIsTowardsLeaf = !marks || marksMatchLeaf(leafEntry[0]); | ||
if (affinityIsTowardsLeaf) { | ||
return leafEntry === backwardLeafEntry ? 'backward' : 'forward'; | ||
} | ||
return 'new-mark'; | ||
} | ||
|
||
const marksDirection: 'forward' | 'backward' | null = | ||
marks && | ||
(() => { | ||
if (backwardLeafEntry && marksMatchLeaf(backwardLeafEntry[0])) | ||
return 'backward'; | ||
if (forwardLeafEntry && marksMatchLeaf(forwardLeafEntry[0])) | ||
return 'forward'; | ||
return null; | ||
})(); | ||
|
||
const selectionDirection = | ||
selection.anchor.offset === 0 ? 'forward' : 'backward'; | ||
|
||
if (selectionDirection === 'backward' && marksDirection === 'forward') | ||
return 'forward'; | ||
|
||
if ( | ||
IS_FIREFOX && | ||
selectionDirection === 'forward' && | ||
marksDirection !== 'backward' | ||
) | ||
return 'forward'; | ||
|
||
return 'backward'; | ||
}; |
This file contains hidden or 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,6 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './getMarkBoundary'; | ||
export * from './getMarkBoundaryAffinity'; |
This file contains hidden or 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,5 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './setMarkBoundaryAffinity'; |
35 changes: 35 additions & 0 deletions
35
packages/mark-affinity/src/transforms/setMarkBoundaryAffinity.ts
This file contains hidden or 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,35 @@ | ||
import { getEndPoint, getNodeProps, PlateEditor } from '@udecode/plate-common'; | ||
import { Point } from 'slate'; | ||
|
||
import { MarkBoundary } from '../types'; | ||
|
||
export const setMarkBoundaryAffinity = ( | ||
editor: PlateEditor, | ||
markBoundary: MarkBoundary, | ||
affinity: 'forward' | 'backward' | ||
) => { | ||
const setMarks = (marks: typeof editor.marks) => { | ||
editor.marks = marks; | ||
editor.onChange(); | ||
}; | ||
|
||
const selectPoint = (point: Point) => { | ||
editor.setSelection({ anchor: point, focus: point }); | ||
}; | ||
if (affinity === 'backward') { | ||
const [backwardLeafEntry] = markBoundary; | ||
if (backwardLeafEntry === null) return setMarks(null); | ||
const endOfBackward = getEndPoint(editor, backwardLeafEntry[1]); | ||
selectPoint(endOfBackward); | ||
setMarks(null); | ||
return; | ||
} | ||
|
||
const [backwardLeafEntry, forwardLeafEntry] = markBoundary; | ||
if (backwardLeafEntry === null) return setMarks(null); | ||
if (forwardLeafEntry === null) return setMarks({}); | ||
|
||
const endOfBackward = getEndPoint(editor, backwardLeafEntry[1]); | ||
selectPoint(endOfBackward); | ||
setMarks(getNodeProps(forwardLeafEntry[0])); | ||
}; |
This file contains hidden or 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,11 @@ | ||
import { TText } from '@udecode/plate-common'; | ||
import { NodeEntry } from 'slate'; | ||
|
||
export interface MarkAffinityPlugin { | ||
validMarks?: string[]; | ||
felixfeng33 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export type MarkBoundary = | ||
| [NodeEntry<TText>, NodeEntry<TText>] | ||
| [NodeEntry<TText>, null] | ||
| [null, NodeEntry<TText>]; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.