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

Rtl Support dynamically when user type first char #1621

Closed
pooooriya opened this issue Jul 20, 2021 · 6 comments
Closed

Rtl Support dynamically when user type first char #1621

pooooriya opened this issue Jul 20, 2021 · 6 comments
Labels
Type: Feature The issue or pullrequest is a new feature

Comments

@pooooriya
Copy link

pooooriya commented Jul 20, 2021

The problem I am facing
when I want to type persian like "سلام"
I Cant change direction
The solution I would like
dynamically change dir and added to classname list of that element
because when I saved my work and use dangerouslyhtml then without any side effects I show my persian language with dir="rtl"

Alternatives I have considered
nothing

Additional context
nothing and thanks for this cool package

@pooooriya pooooriya added Type: Feature The issue or pullrequest is a new feature v2 labels Jul 20, 2021
@cadars
Copy link
Contributor

cadars commented Jul 21, 2021

See: #116

Still, if not default, it would be nice to have dir="auto" as a simple setting.

@cadars
Copy link
Contributor

cadars commented Jul 21, 2021

Following the guide for global attributes I came up with a quick and dirty extension. (For some reason lists and blockquotes only work in Firefox.)

import { Extension } from '@tiptap/core'

const AutoDir = Extension.create({
  addGlobalAttributes() {
    return [   
      {
        types: [
          'heading',
          'paragraph',
          'blockquote',
          'bulletList',
          'orderedList',
        ],
        attributes: {
          autoDir: {
            renderHTML: attributes => ({
              dir: 'auto',
            }),
            parseHTML: element => ({
              autoDir: element.dir.autoDir || 'auto',
            }),
          },
        },
      },
    ]
  },
})

@cadars
Copy link
Contributor

cadars commented Aug 4, 2021

Update: to make blockquotes and lists work, dir="auto" should only be added to parent nodes… I’m not sure how to make that happen in the extension.

Here is a test for this issue: https://codepen.io/cadars/pen/poeXMXZ?editors=1100

@cadars
Copy link
Contributor

cadars commented Sep 14, 2021

Updated to work with the parseHTML method change:

import { Extension } from '@tiptap/core'

const AutoDir = Extension.create({
  name: 'AutoDir',
  addGlobalAttributes() {
    return [   
      {
        types: [
          'heading',
          'paragraph',
          'bulletList',
          'orderedList',
          'blockquote',
        ],
        attributes: {
          autoDir: {
            renderHTML: attributes => ({
              dir: 'auto',
            }),
            parseHTML: element => element.dir || 'auto',
          },
        },
      },
    ]
  },
})

@hanspagel hanspagel removed the v2 label Sep 28, 2021
@cadars cadars mentioned this issue Oct 5, 2021
6 tasks
@philippkuehn
Copy link
Contributor

For now, adding a custom extension is the preferred way.

@amirhhashemi
Copy link
Contributor

Hey everyone. I wrote a small extension that automatically adds dir="rtl" or dir="ltr" based on the node's content. You might find it useful: https://github.com/amirhhashemi/tiptap-text-direction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature The issue or pullrequest is a new feature
Projects
None yet
Development

No branches or pull requests

5 participants