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(dom-adapters): basic inline tool adapter implementation #74

Merged
merged 55 commits into from
Aug 29, 2024

Conversation

e11sy
Copy link
Contributor

@e11sy e11sy commented Aug 27, 2024

Inline tool adapter implementation

structure:

  • Editor (playground simulation)
  • InlineTool instanse (one for editor, for each inline tool)
  • InlineToolbar toolbar that is rendered with inline tools available inside block
    • Business logic : (InlineToolbar class)
    • UI : Toolbar component
  • InlineToolAdapter - adapter, that connects model and editor
  • model stores data

NOTE:

  • tool should determine it's state for selection

Progress

  • Implement playground simulation
  • InlineToolbar
    • Business logic
      • Subscribe on selection change
      • Validate inline tools available in current block
    • UI
      • Simple realisation
      • Support additional data for inline tools (e.g. link tool requires link)
  • InlineToolAdapter
    • Get action and range from tool instance
    • Subscribe on model changes
  • Model support intersectType for inline tools
enum IntersectType {
  Extend = 'extend',           // if two fragments of one tool intersect - merge into one fragment
  Replace = 'replace',         // if two new fragment intersect with existing - remove existing with new one
  Separate = 'separate'        // if two fragments of one tool intersect - treat them as two different fragments
}
  • Tests
    image

Next

  • Validate inline tools that are available in current block
  • Support additional data for inline tools
  • Support IntersectType for inline tools
  • Add ability to unformat inline tool in dom (that should do tool with adapter)
  • Caret should reset state of selection if no selection exists
  • Cover with tests

Copy link

github-actions bot commented Aug 27, 2024

⏭️ No files to mutate for ./packages/model

Copy link

github-actions bot commented Aug 27, 2024

Coverage report for ./packages/dom-adapters

St.
Category Percentage Covered / Total
🟢 Statements 100% 0/0
🟢 Branches 100% 0/0
🟢 Functions 100% 0/0
🟢 Lines 100% 0/0

Test suite run success

1 tests passing in 1 suite.

Report generated by 🧪jest coverage report action from 6db4d90

Copy link

github-actions bot commented Aug 27, 2024

Coverage report for ./packages/model

St.
Category Percentage Covered / Total
🟢 Statements 100% 753/753
🟢 Branches
99.5% (+0.01% 🔼)
201/202
🟢 Functions
98.37% (-1.07% 🔻)
181/184
🟢 Lines 100% 725/725
Show new covered files 🐣
St.
File Statements Branches Functions Lines
🟢
... / IntersectType.ts
100% 100% 100% 100%
🟢
... / FormattingAction.ts
100% 100% 100% 100%
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢
... / index.ts
100% 100%
50% (-50% 🔻)
100%

Test suite run success

389 tests passing in 24 suites.

Report generated by 🧪jest coverage report action from 6db4d90

Copy link

github-actions bot commented Aug 27, 2024

⏭️ No files to mutate for ./packages/dom-adapters

@e11sy e11sy changed the title [WIP] inline tool adapter feat(inlineToolAdapter): basic inline tool adapter implementation Aug 28, 2024
@e11sy e11sy changed the title feat(inlineToolAdapter): basic inline tool adapter implementation feat(dom-adapters): basic inline tool adapter implementation Aug 28, 2024

const inlineElement = tool.create();

range.surroundContents(inlineElement);
Copy link
Member

Choose a reason for hiding this comment

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

That would throw an exception in some cases, see https://developer.mozilla.org/en-US/docs/Web/API/Range/surroundContents

Comment on lines 119 to 121
const textRange = index.textRange!;
const blockIndex = index.blockIndex!;
const dataKey = index.dataKey!;
Copy link
Member

Choose a reason for hiding this comment

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

Non-null assertion could be dangerous here, would be better to check each index part


const fragments = this.#model.getFragments(blockIndex, dataKey, ...textRange, toolName);

const { action, range } = tool.getAction(textRange, fragments, intersectType, data);
Copy link
Member

Choose a reason for hiding this comment

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

Tool will know it's intersect type by itself, no need to pass it here. It should be passed to the model when we'll implement different intersections support

* @returns {boolean} true if itersect exists, false otherwise
*/
export function intersectionExists(firstRange: TextRange, secondRange: TextRange): boolean {
return (firstRange[0] < secondRange[1] && firstRange[0] > secondRange[0] || firstRange[1] < secondRange[1] && firstRange[1] > secondRange[0]);
Copy link
Member

Choose a reason for hiding this comment

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

You can use a formula

(firstRange[0] - secondRange[1]) * (secondRange[0] - firstRange[1]) > 0 

* @param secondRange - second text range
* @returns {boolean} true if itersect exists, false otherwise
*/
export function intersectionExists(firstRange: TextRange, secondRange: TextRange): boolean {
Copy link
Member

Choose a reason for hiding this comment

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

Also let's rename to smth like doRangesIntersect

* @param secondRange - second range to be merged
* @returns {TextRange} merged text range
*/
export function mergeTextRanges(firstRange: TextRange, secondRange: TextRange): TextRange {
Copy link
Member

Choose a reason for hiding this comment

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

Would be great to check if ranges intersects right inside this function

Comment on lines 69 to 76
if (selection) {
/**
* Do not render inline toolbar for not contenteditable elements
*/
if (selection.focusNode?.nodeType !== Node.TEXT_NODE) {
return;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Would be enough to check if caret has textRange. If it has it — that means caret is in the text input

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as i can see, caret also has text range for native inputs, but i want to check that current node is text and contenteditable

* Class handles on format model events and renders inline tools
* Applies format to the model
*/
export class InlineToolAdapter {
Copy link
Member

Choose a reason for hiding this comment

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

If it would be a single adapter for all of the tools, let's name it InlineToolsAdapter

packages/core/src/entities/InlineTool.ts Outdated Show resolved Hide resolved
packages/core/src/entities/InlineTool.ts Outdated Show resolved Hide resolved
packages/core/src/entities/InlineTool.ts Outdated Show resolved Hide resolved
packages/core/src/entities/InlineTool.ts Outdated Show resolved Hide resolved
packages/core/src/entities/InlineTool.ts Outdated Show resolved Hide resolved
packages/core/src/ui/InlineToolbar/index.ts Outdated Show resolved Hide resolved
packages/core/src/ui/InlineToolbar/index.ts Outdated Show resolved Hide resolved
packages/dom-adapters/src/InlineToolsAdapter/index.ts Outdated Show resolved Hide resolved
packages/dom-adapters/src/InlineToolsAdapter/index.ts Outdated Show resolved Hide resolved
Comment on lines 40 to 48
/**
* Formatting action - format or unformat
*/
action: FormattingAction;

/**
* Range to apply formatting action
*/
range: TextRange;
Copy link
Member

Choose a reason for hiding this comment

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

Let's extract this to a separate intereface

Comment on lines 64 to 73
const selection = window.getSelection();

/**
* Get current input with selection
*/
if (selection) {
/**
* Do not render inline toolbar for not contenteditable elements
*/
if (selection.focusNode?.nodeType !== Node.TEXT_NODE) {
Copy link
Member

Choose a reason for hiding this comment

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

I left same comment previously, you should be able to just check if index has a text range, no need to check node type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated comments

/**
* If two fragments of one tool intersect - treat them as two different fragments
*/
Separate = 'separate'
Copy link
Member

Choose a reason for hiding this comment

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

I don't really like that name, I think it's not descriptive enough. Maybe something like LeaveBoth or None as there's no intersection in that case. Happy to discuss other ideas

/**
* If two fragments of one tool intersect - treat them as two different fragments
*/
LeaveBoth = 'LeaveBoth'
Copy link
Member

Choose a reason for hiding this comment

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

To be consistent

Suggested change
LeaveBoth = 'LeaveBoth'
LeaveBoth = 'leave-both'

packages/core/src/entities/InlineTool.ts Outdated Show resolved Hide resolved
packages/core/src/entities/InlineTool.ts Outdated Show resolved Hide resolved
packages/core/src/tools/ToolsManager.ts Outdated Show resolved Hide resolved
packages/playground/tsconfig.json Outdated Show resolved Hide resolved
@e11sy e11sy enabled auto-merge August 29, 2024 15:06
@e11sy e11sy added this pull request to the merge queue Aug 29, 2024
Merged via the queue into main with commit 50f0db4 Aug 29, 2024
12 checks passed
@e11sy e11sy deleted the inline-tool-adapter branch August 29, 2024 18:04
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.

3 participants