GithubHelp home page GithubHelp logo

amirhhashemi / tiptap-text-direction Goto Github PK

View Code? Open in Web Editor NEW
29.0 2.0 1.0 71 KB

Text direction extension for Tiptap

Home Page: https://www.npmjs.com/package/tiptap-text-direction

License: MIT License

TypeScript 100.00%
arabic farsi hebrew prosemirror right-to-left rtl tiptap tiptap-editor tiptap-v2

tiptap-text-direction's Introduction

This extension automatically detects the direction of a configurable list of nodes and adds dir="ltr" or dir="rtl" to them.

Why not dir="auto"?

dir="auto" changes the text direction based on the element's content too, so why not use that?

  1. It doesn't give you granular control over the direction. For example, if you want to have different styles based on the direction you can't do that with dir="auto". There is :dir() pseudo-class that can help you in this situation but it's only supported in Firefox.

  2. You can't override it. dir="auto" uses the first character of the element to determine the direction and you can't change it unless you explicitly set the direction with dir="ltr|rtl".

Installation

# npm
npm install tiptap-text-direction

# yarn
yarn add tiptap-text-direction

# pnpm
pnpm install tiptap-text-direction

Usage

In this example I used React but it works with any framework that Tiptap supports.

import { useEditor, EditorContent } from "@tiptap/react";
import StarterKit from "@tiptap/starter-kit";
import TextDirection from "tiptap-text-direction";

const Tiptap = () => {
  const editor = useEditor({
    extensions: [
      StarterKit,
      TextDirection.configure({
        types: ["heading", "paragraph"],
      }),
    ],
  });

  return <EditorContent editor={editor} />;
};

You might also want to change the text alignment based on the dir attribute:

.ProseMirror p[dir="rtl"],
.ProseMirror h1[dir="rtl"],
.ProseMirror h2[dir="rtl"],
.ProseMirror h3[dir="rtl"],
.ProseMirror h4[dir="rtl"],
.ProseMirror h5[dir="rtl"],
.ProseMirror h6[dir="rtl"] {
  text-align: right;
}

.ProseMirror p[dir="ltr"],
.ProseMirror h1[dir="ltr"],
.ProseMirror h2[dir="ltr"],
.ProseMirror h3[dir="ltr"],
.ProseMirror h4[dir="ltr"],
.ProseMirror h5[dir="ltr"],
.ProseMirror h6[dir="ltr"] {
  text-align: left;
}

Demo

2022-07-09.20-44-39.mp4

HTML Output

In this example the defaultDirection is set to rtl (also a parent element has dir="rtl", in this case the <html> tag) so the extension didn't add dir="rtl" to RTL nodes.

<p dir="ltr">Hello</p>
<p dir="ltr">سلام hello</p>
<!-- This was `rtl` by default but we forced it be `ltr` -->
<ul>
  <li>
    <p dir="ltr">hello</p>
  </li>
  <li>
    <p>سلام</p>
  </li>
  <li>
    <p dir="ltr">sghl</p>
  </li>
</ul>
<h2>سلام</h2>
<h2 dir="ltr">hello</h2>

Options

types

A list of nodes where the dir attribute should be added to.

Default: []

TextDirection.configure({
  types: ["heading", "paragraph"],
});

defaultDirection

In case you have set the text direction in a parent element of the editor (most likely the <html> element), you can set defaultDirection to avoid adding the dir attribute to elements that have the same direction as the defaultDirection because it's not needed. It can reduce the HTML output's size.

Default: null

TextDirection.configure({
  defaultDirection: "rtl",
});

Commands

setTextDirection()

Set the text direction of the selected nodes to the specified value.

editor.commands.setTextDirection("rtl");

unsetTextDirection()

Unset the text direction back to the defaultDirection.

editor.commands.unsetTextDirection();

Keyboard shortcuts

Command Windows/Linux macOS
setTextDirection("ltr") Ctrl + Alt + l Cmd + Alt + l
setTextDirection("rtl") Ctrl + Alt + r Cmd + Alt + r

tiptap-text-direction's People

Contributors

amirhhashemi avatar github-actions[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

svejdani

tiptap-text-direction's Issues

The text styles are being reset when a line break is inserted

Hi there,

Upon installing this extension, the text styles are being reset when a line break is inserted by pressing the enter key, or when text is deleted without removing the entire line itself.
With

The expected behavior is
Without

Any insights are greatly appreciated!

How to detect current direction ?

Hello,

Kindly, need a way to detect active current direction (rtl / ltr), i tried following:

editor.isActive({ textDirection: "rtl" });

but always returns false even if it's already current active direction !

How to apply in span tag?

Hi, thanks for this awesome extension.
Can you help how to apply the dir in span tag?

So, I have a content like <span class="text-arabic">...</span> and I want to add the dir="rtl" to it.
Currently it will wrapped with a <p> tag, so it make:

<p dir="rtl">
  <span class="text-arabic">...</span>
</p>

I want make it:

<span class="text-arabic" dir="rtl">...</span>

Thanks in advance.

[Bug] Punctuations and other characters

Hello,

I was wondering if it's possible to properly align punctuations when writing English in RTL text.

Example:
image

As you can see the question mark is on the wrong side and it behaves the same in the TipTap editor.

Let me know,
Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.