Skip to content

Commit a95e236

Browse files
committed
removing wrapper
1 parent 97f04e6 commit a95e236

6 files changed

Lines changed: 63 additions & 31 deletions

File tree

‎build/codex-editor.js‎

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎build/codex-editor.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎example/tools-inline/marker/marker.js‎

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
/**
22
* @todo remove wrapper if selection () inside it:
3-
* s([ample tex])t -> sample text
4-
* s[ampl(e t)ex]t -> s[ampl]e t[ex]t @todo create splitter
5-
* s(ampl[e t]ex)t -> s[ampl[e t]ex]t do not remove wrapper
6-
* s([ampl]e t[ex])t -> s[[ampl]e t[ex]]t do not remove wrapper
3+
* [x] s[(ample tex)]t -> sample text
4+
* [x] s[ampl(e t)ex]t -> sample text
5+
* (?) s[ampl(e t)ex]t -> s[ampl]e t[ex]t @todo create splitter
6+
* [x] s(ampl[e t]ex)t -> s[ampl[e t]ex]t add remove wrapper
7+
* [ ] s[(ampl]e t[ex)]t -> s[[ampl]e t[ex]]t add wrapper
78
*
89
* @todo process cross-tag wrapping []:
910
* sam[ple <b>te]xt</b> -> sam[ple ]<b>[te]xt</b>
@@ -12,16 +13,14 @@
1213
* sa[mple t][ex]t -> sa[mple tex]t
1314
* sa[mpl[e t]ex]t -> sa[mple tex]t
1415
* @see https://developer.mozilla.org/en-US/docs/Web/API/Range/commonAncestorContainer
15-
*
16-
* @todo save selection after clicking on tool
1716
*/
1817

1918
class Marker {
2019
constructor(api) {
2120
this.api = api;
22-
console.log(this.api);
2321

2422
this.button = null;
23+
this.TAG = 'SPAN';
2524
this.CSS = 'marked';
2625
}
2726

@@ -44,32 +43,43 @@ class Marker {
4443

4544
let state = this.checkState();
4645

46+
/**
47+
* If start or end of selection is in the highlighted block
48+
*/
4749
if (state) {
48-
console.log('range', range);
49-
console.log('state', state);
50-
51-
/** @todo remove highlighting */
52-
53-
// /**
54-
// *
55-
// */
56-
// if (range.startContainer === range.endContainer) {
57-
// /**
58-
// * @todo remove whole wrapper not class
59-
// */
60-
// state.classList.remove(this.CSS);
61-
// }
50+
/**
51+
* Expand selection
52+
*/
53+
this.api.selection.expandToTag(state);
54+
55+
/**
56+
* Remove wrapper
57+
*/
58+
state.outerHTML = state.innerHTML;
59+
60+
// @todo save selection on the text
6261
} else {
63-
let span = document.createElement('SPAN');
62+
/**
63+
* Create a wrapper for highlighting
64+
*/
65+
let span = document.createElement(this.TAG);
6466

6567
span.classList.add(this.CSS);
68+
69+
/**
70+
* Wrap text with wrapper tag
71+
*/
6672
range.surroundContents(span);
67-
range.selectNode(span);
73+
74+
/**
75+
* Expand (add) selection to highlighted block
76+
*/
77+
this.api.selection.expandToTag(span);
6878
}
6979
}
7080

7181
checkState(selection) {
72-
const markerTag = this.api.selection.findParentTag('SPAN', this.CSS);
82+
const markerTag = this.api.selection.findParentTag(this.TAG, this.CSS);
7383

7484
this.button.classList.toggle('ce-inline-tool--active', !!markerTag);
7585

‎src/components/interfaces/api.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,17 @@ export interface ISelectionAPI {
8383
/**
8484
* Looks ahead to find passed tag from current selection
8585
*
86-
* @param tagName
87-
* @param className
86+
* @param {String} tagName
87+
* @param {String} className
8888
*/
8989
findParentTag: (tagName: string, className: string) => Node;
90+
91+
/**
92+
* Expands selection range to the passed parent node
93+
*
94+
* @param {HTMLElement} node
95+
*/
96+
expandToTag: (node: HTMLElement) => void;
9097
}
9198

9299
/**

‎src/components/modules/api-selection.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ export default class SelectionAPI extends Module implements ISelectionAPI {
2323
*/
2424
get methods(): ISelectionAPI {
2525
return {
26-
findParentTag: (tagName, className) => this.findParentTag(tagName, className),
26+
findParentTag: (tagName: string, className: string) => this.findParentTag(tagName, className),
27+
expandToTag: (node: HTMLElement) => this.expandToTag(node),
2728
};
2829
}
2930

30-
public findParentTag(tagName, className) {
31+
public findParentTag(tagName: string, className: string): Node {
3132
return new Selection().findParentTag(tagName, className);
3233
}
3334

35+
public expandToTag(node: HTMLElement): void {
36+
new Selection().expandToTag(node);
37+
}
38+
3439
}

‎src/components/selection.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ export default class Selection {
242242

243243
/**
244244
* Expands selection range to the passed parent node
245-
* @param {Element} node
245+
*
246+
* @param {HTMLElement} node
246247
*/
247248
expandToTag(node) {
248249
let selection = window.getSelection();

0 commit comments

Comments
 (0)