Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Apr 23, 2024
1 parent 9663c26 commit 665df11
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 54 deletions.
2 changes: 1 addition & 1 deletion apps/www/src/registry/default/example/playground-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export const usePlaygroundPlugins = ({
createStrikethroughPlugin({ enabled: !!enabled.strikethrough }),
createMarkAffinityPlugin({
options: {
pressRightArrowAtBoundary: (editor, currentEndLeafEntry) => {
moveRightAtBoundary: (editor, currentEndLeafEntry) => {
console.log(editor, 'fj');
console.log(currentEndLeafEntry, 'fj');
},
Expand Down
69 changes: 17 additions & 52 deletions packages/affinity-mark/src/createMarkAffinityPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import {
createPluginFactory,
getPluginOptions,
isEndPoint,
isSelectionExpanded,
isStartPoint,
} from '@udecode/plate-common';

import { getMarkBoundary } from './getMarkBoundary';
import { getMarkBoundaryAffinity } from './getMarkBoundaryAffinity';
import { setMarkBoundaryAffinity } from './setMarkBoundaryAffinity';
import { MarkAffinityPlugin, MarkBoundary } from './types';

const MARK_CODE = 'code';

const markBoundaryHasMark = (markBoundary: MarkBoundary, mark: string) => {
const [backwardLeafEntry, forwardLeafEntry] = markBoundary;
return (
(backwardLeafEntry && backwardLeafEntry[0][mark]) ||
(forwardLeafEntry && forwardLeafEntry[0][mark])
);
};
import { MarkAffinityPlugin } from './types';

export const KEY_MARK_AFFINITY = 'mark-affinity';

Expand Down Expand Up @@ -66,36 +58,13 @@ export const createMarkAffinityPlugin = createPluginFactory<MarkAffinityPlugin>(
distance = 1,
reverse = false,
} = options || {};

if (
unit === 'character' &&
distance === 1 &&
editor.selection &&
!isSelectionExpanded(editor)
) {
// const beforeMarkBoundary = getMarkBoundary(editor);

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

// move(options);

const afterMarkBoundary = getMarkBoundary(editor);

if (!afterMarkBoundary) return move(options);
Expand All @@ -106,27 +75,23 @@ export const createMarkAffinityPlugin = createPluginFactory<MarkAffinityPlugin>(

const currentEndLeafEntry = afterMarkBoundary[0];
const nextLeafEntry = afterMarkBoundary[1];
const { pressRightArrowAtBoundary } =
getPluginOptions<MarkAffinityPlugin>(editor, KEY_MARK_AFFINITY);

if (!pressRightArrowAtBoundary) return move(options);

if (!isRight) return move(options);
const { moveRightAtBoundary, moveLeftAtBoundary } =
getPluginOptions<MarkAffinityPlugin>(editor, KEY_MARK_AFFINITY);

const node = editor;
console.log('🚀 ~ node:', node);
const point = editor.selection.anchor;
const isEnd = isEndPoint(editor, point, point.path);
const isStart = isStartPoint(editor, point, point.path);

pressRightArrowAtBoundary(
editor,
currentEndLeafEntry!,
nextLeafEntry!
);
if (isRight && isEnd && moveRightAtBoundary) {
moveRightAtBoundary(editor, currentEndLeafEntry!, nextLeafEntry!);
return console.log('rrr');
}

// setMarkBoundaryAffinity(
// editor,
// afterMarkBoundary,
// reverse ? 'forward' : 'backward'
// );
if (!isRight && isStart && moveLeftAtBoundary) {
moveLeftAtBoundary(editor, currentEndLeafEntry!, nextLeafEntry!);
return console.log('llll');
}
}

move(options);
Expand Down
7 changes: 6 additions & 1 deletion packages/affinity-mark/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { TText } from '@udecode/plate-common';
import { NodeEntry } from 'slate';

export interface MarkAffinityPlugin {
pressRightArrowAtBoundary?: (
moveRightAtBoundary?: (
editor: any,
currentEndLeafEntry?: NodeEntry<TText>,
nextLeafEntry?: NodeEntry<TText>
) => void;
moveLeftAtBoundary?: (
editor: any,
currentEndLeafEntry?: NodeEntry<TText>,
nextLeafEntry?: NodeEntry<TText>
Expand Down

0 comments on commit 665df11

Please sign in to comment.