-
Notifications
You must be signed in to change notification settings - Fork 817
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
base: main
Are you sure you want to change the base?
Feat/mark affinity #3147
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__tests__ | ||
__test-utils__ | ||
__mocks__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Plate marks affinity plugins | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
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) |
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", | ||||||||||||||
Comment on lines
+2
to
+4
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
"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" | ||||||||||||||
} | ||||||||||||||
} |
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, | ||
} | ||
); |
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'; |
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]; | ||
}; |
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'; | ||
}; |
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'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/** | ||
* @file Automatically generated by barrelsby. | ||
*/ | ||
|
||
export * from './setMarkBoundaryAffinity'; |
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])); | ||
}; |
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[]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
} | ||
|
||
export type MarkBoundary = | ||
| [NodeEntry<TText>, NodeEntry<TText>] | ||
| [NodeEntry<TText>, null] | ||
| [null, NodeEntry<TText>]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.