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

Fix quill for creately #2

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a763a4f
Retain active format for empty lines
thisunravisara May 28, 2020
913fb53
Prettyfied
thisunravisara May 28, 2020
d055425
Updated a vairable name
thisunravisara May 28, 2020
a30d7a9
Updated block js
thisunravisara May 31, 2020
1fa7235
Added a hook to modify pasted delta and updated optimize methid in bl…
thisunravisara Jun 22, 2020
d46ef3c
Added dist files
thisunravisara Jun 22, 2020
44dc142
Updated dist files
thisunravisara Jun 22, 2020
e76443e
es5
thisunravisara Jun 22, 2020
8a37869
Updated webpack to support chrome 58
thisunravisara Jun 26, 2020
e9f3e79
Commit build files
thisunravisara Jun 26, 2020
c73d500
Updated editor.js and block.js
thisunravisara Jul 2, 2020
b658f0d
Update build files and package version
thisunravisara Jul 3, 2020
0519106
Fixed text is bold when pasted from google doc
thisunravisara Jul 17, 2020
3d6d563
ignoring new shortcut property based on keys
ltrefai Aug 24, 2020
636d69b
updated version for quill
ltrefai Aug 24, 2020
3bbafb7
Merge pull request #3 from creately/feat-addShortcutCheck
thisunravisara Sep 7, 2020
8f42b90
changed naming
ltrefai Oct 14, 2020
a50f474
updated package version
ltrefai Oct 14, 2020
c2ecbda
reverted package lock changes
ltrefai Oct 14, 2020
2ba66a0
Merge pull request #4 from creately/fix-shortcutNameChange
thisunravisara Oct 14, 2020
d8cb77c
Retain formats of emply line
mrumaiz94 Jan 21, 2021
3a5d594
Merge pull request #5 from creately/fix-retain-format-empty-text
mrumaiz94 Jan 29, 2021
0c1bbab
fix text triple click line delete issue
mrumaiz94 Feb 23, 2021
5756546
Merge pull request #6 from creately/fix-tripleclick-line-delete
mrumaiz94 Feb 24, 2021
78eb9cb
update parchement version to fix console error after fix retain empty…
mrumaiz94 Mar 23, 2021
5d66faf
Merge pull request #7 from creately/update-parchment-version
mrumaiz94 Mar 23, 2021
642b4e0
Update @creately/parchment version to 1.0.2
mrumaiz94 Apr 28, 2021
be97552
Merge pull request #8 from creately/fix-text-disappear-on-edit-parchment
mrumaiz94 Apr 28, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.*
/dist
/node_modules
/selenium
/docs/_site
Expand Down
2 changes: 1 addition & 1 deletion _develop/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ const jsRules = {
{
targets: {
browsers: [
'last 2 Chrome major versions',
'last 2 Firefox major versions',
'last 2 Safari major versions',
'last 2 Edge major versions',
'last 2 iOS major versions',
'last 2 ChromeAndroid major versions',
],
chrome: "58",
},
},
],
Expand Down
125 changes: 122 additions & 3 deletions blots/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,25 @@ import {
EmbedBlot,
LeafBlot,
Scope,
} from 'parchment';
} from '@creately/parchment';
import Break from './break';
import Inline from './inline';
import TextBlot from './text';
import Cursor from './cursor';

const NEWLINE_LENGTH = 1;
const TEXT_FORMAT_NODES = [
'SPAN',
'STRONG',
'B',
'EM',
'I',
'SUB',
'SUP',
'S',
'STRIKE',
'U',
];

class Block extends BlockBlot {
constructor(scroll, domNode) {
Expand Down Expand Up @@ -95,9 +108,109 @@ class Block extends BlockBlot {

optimize(context) {
super.optimize(context);

/**
* FIXME
* This is a tempory change to fix following
* 1. Can't add multuple new lines without losing the format.
* 2. When setting content, the new lines are always formatless.
* 3. Deleting a line should not result in clearing it's formats. => Fixed in parchment as permenent
* 4. Cursor height / line height should be consistent unless user change formatting.
*
* In super method default blot ("BR") is appended if this blot is empty.
* Since this "Block" class is extended by some other classes
* this.domNode.nodeName === "P" is checked here but
* a sperate child class should be added for "P" block and do this change
*/
try {
if(this.domNode.innerText === '' && this.children.length !== 0){
const leafBlot = getDescendentsOfLeafBlot(this)[0];
const lastElementBlot = leafBlot.parent;
if (leafBlot instanceof TextBlot) {
leafBlot.remove();
const child = this.scroll.create(Break.blotName);
lastElementBlot.appendChild(child);
}
}

if( this.isFormatlessEmptyP( this.domNode )) {
let formatNode;
if ( this.prev && !this.isFormatlessEmptyP( this.prev.domNode )) {
formatNode = this.prev.children.tail.domNode.cloneNode(true);
} else if ( this.next && !this.isFormatlessEmptyP( this.next.domNode ) ) {
formatNode = this.next.children.head.domNode.cloneNode(true);
}
if( formatNode && formatNode.nodeType === Node.ELEMENT_NODE ) {
this.children.head.domNode.remove();
this.removeChild( this.children.head );
this.retainFormats(formatNode);
const newBr = document.createElement('BR');
const dn = this.getDeepestNode( formatNode );
dn.appendChild( newBr );
const formatBlot = this.scroll.create(formatNode);
this.appendChild( formatBlot );
}
}
if( !this.isFormatlessEmptyP( this.domNode ) && this.prev && this.isFormatlessEmptyP( this.prev.domNode )) {
const formatNode = this.children.head.domNode.cloneNode(true);
this.applyToAllEmptyPrevs( this.prev, formatNode );
}

} catch (error) {
}
this.cache = {};
}

isFormatlessEmptyP( node ) {
return node.nodeName === "P" && node.innerHTML === `<br>`;
}

applyToAllEmptyPrevs( prev, formatNode ) {
if ( prev && this.isFormatlessEmptyP( prev.domNode )) {
if( formatNode.nodeType === Node.ELEMENT_NODE ) {
prev.children.head.domNode.remove();
prev.removeChild( prev.children.head );
this.retainFormats(formatNode);
const newBr = document.createElement('BR');
const dn = prev.getDeepestNode( formatNode );
dn.appendChild( newBr );
const formatBlot = prev.scroll.create(formatNode);
prev.appendChild( formatBlot );
}
this.applyToAllEmptyPrevs( prev.prev, formatNode );
}

}

/**
* Retains the style nodes specified in TEXT_FORMAT_NODES
* and removes all other nodes for the given element
*/
retainFormats(element) {
const nodes = element.childNodes;
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
const isCursorSpan = node.nodeName === "SPAN" && node.classList.contains( Cursor.className );
if (
isCursorSpan ||
node.nodeType === Node.TEXT_NODE ||
!TEXT_FORMAT_NODES.includes(node.nodeName)
) {
node.parentNode.removeChild(node);
i -= 1;
} else if (node.nodeType === Node.ELEMENT_NODE) {
this.retainFormats(node);
}
}
}

getDeepestNode(node) {
if ( node.firstChild ) {
return this.getDeepestNode(node.firstChild);
}
return node;
}

path(index) {
return super.path(index, true);
}
Expand Down Expand Up @@ -165,8 +278,7 @@ BlockEmbed.scope = Scope.BLOCK_BLOT;
// It is important for cursor behavior BlockEmbeds use tags that are block level elements

function blockDelta(blot, filter = true) {
return blot
.descendants(LeafBlot)
return getDescendentsOfLeafBlot(blot)
.reduce((delta, leaf) => {
if (leaf.length() === 0) {
return delta;
Expand Down Expand Up @@ -198,4 +310,11 @@ function bubbleFormats(blot, formats = {}, filter = true) {
return bubbleFormats(blot.parent, formats, filter);
}

function getDescendentsOfLeafBlot (blot) {
if(!blot){
return;
}
return blot.descendants(LeafBlot);
}

export { blockDelta, bubbleFormats, BlockEmbed, Block as default };
2 changes: 1 addition & 1 deletion blots/break.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBlot } from 'parchment';
import { EmbedBlot } from '@creately/parchment';

class Break extends EmbedBlot {
static value() {
Expand Down
2 changes: 1 addition & 1 deletion blots/container.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContainerBlot } from 'parchment';
import { ContainerBlot } from '@creately/parchment';

class Container extends ContainerBlot {}

Expand Down
2 changes: 1 addition & 1 deletion blots/cursor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBlot, Scope } from 'parchment';
import { EmbedBlot, Scope } from '@creately/parchment';
import TextBlot from './text';

class Cursor extends EmbedBlot {
Expand Down
2 changes: 1 addition & 1 deletion blots/embed.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBlot } from 'parchment';
import { EmbedBlot } from '@creately/parchment';
import TextBlot from './text';

const GUARD_TEXT = '\uFEFF';
Expand Down
2 changes: 1 addition & 1 deletion blots/inline.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBlot, InlineBlot, Scope } from 'parchment';
import { EmbedBlot, InlineBlot, Scope } from '@creately/parchment';
import Break from './break';
import Text from './text';

Expand Down
2 changes: 1 addition & 1 deletion blots/scroll.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Scope, ScrollBlot, ContainerBlot } from 'parchment';
import { Scope, ScrollBlot, ContainerBlot } from '@creately/parchment';
import Emitter from '../core/emitter';
import Block, { BlockEmbed } from './block';
import Break from './break';
Expand Down
2 changes: 1 addition & 1 deletion blots/text.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TextBlot } from 'parchment';
import { TextBlot } from '@creately/parchment';

class Text extends TextBlot {}

Expand Down
2 changes: 1 addition & 1 deletion core/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cloneDeep from 'lodash.clonedeep';
import isEqual from 'lodash.isequal';
import merge from 'lodash.merge';
import Delta, { AttributeMap } from 'quill-delta';
import { LeafBlot } from 'parchment';
import { LeafBlot } from '@creately/parchment';
import { Range } from './selection';
import CursorBlot from '../blots/cursor';
import Block, { BlockEmbed, bubbleFormats } from '../blots/block';
Expand Down
13 changes: 12 additions & 1 deletion core/quill.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Delta from 'quill-delta';
import cloneDeep from 'lodash.clonedeep';
import merge from 'lodash.merge';
import * as Parchment from 'parchment';
import * as Parchment from '@creately/parchment';
import Editor from './editor';
import Emitter from './emitter';
import Module from './module';
Expand Down Expand Up @@ -404,6 +404,17 @@ class Quill {
if (index == null) {
this.selection.setRange(null, length || Quill.sources.API);
} else {
if (index instanceof Range) {
const lines = this.scroll.lines();
let currIndex = 0;
for ( let i = 0; i < lines.length; i ++ ) {
if ( currIndex === index.index && lines[i].length() === index.length ) {
index.length = index.length - 1;
break;
}
currIndex = currIndex + lines[i].length();
}
}
[index, length, , source] = overload(index, length, source);
this.selection.setRange(new Range(Math.max(0, index), length), source);
if (source !== Emitter.sources.SILENT) {
Expand Down
2 changes: 1 addition & 1 deletion core/selection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LeafBlot, Scope } from 'parchment';
import { LeafBlot, Scope } from '@creately/parchment';
import cloneDeep from 'lodash.clonedeep';
import isEqual from 'lodash.isequal';
import Emitter from './emitter';
Expand Down
Loading