Skip to content

Commit

Permalink
change marker type to "marker", use "tagName" in markup
Browse files Browse the repository at this point in the history
  • Loading branch information
bantic committed Jul 19, 2015
1 parent d83302d commit 875d31c
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
25 changes: 13 additions & 12 deletions src/js/models/marker.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const MARKUP_TYPES = ['b', 'a', 'i', 'em', 'strong'];
export const MARKUP_TAG_NAMES = ['b', 'a', 'i', 'em', 'strong'];
const MARKER_TYPE = 'marker';

import { detect } from 'content-kit-editor/utils/array-utils';
Expand Down Expand Up @@ -27,28 +27,29 @@ const Marker = class Marker {
addMarkup(markup) {
// simple markup, no attributes
if (typeof markup === 'string') {
markup = {type: markup};
markup = {tagName: markup};
}
let {type, attributes} = markup;
type = type.toLowerCase();
let {tagName, attributes} = markup;
tagName = tagName.toLowerCase();

if (MARKUP_TYPES.indexOf(type) === -1) {
throw new Error(`Cannot add markup of type ${type}`);
if (MARKUP_TAG_NAMES.indexOf(tagName) === -1) {
throw new Error(`Cannot add markup of tagName ${tagName}`);
}

markup = {type, attributes};
markup = {tagName, attributes};

if (!this.hasMarkup(type)) {
if (!this.hasMarkup(tagName)) {
this.markups.push(markup);
}
}

hasMarkup(type) {
return detect(this.markups, markup => markup.type === type);
hasMarkup(tagName) {
tagName = tagName.toLowerCase();
return detect(this.markups, markup => markup.tagName === tagName);
}

getMarkup(type) {
return this.hasMarkup(type);
getMarkup(tagName) {
return this.hasMarkup(tagName);
}

join(other) {
Expand Down
4 changes: 4 additions & 0 deletions src/js/parsers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ export default {
});

return post;
},

parseSection(element) {
return SectionParser.parse(element);
}
};
6 changes: 3 additions & 3 deletions src/js/parsers/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from 'content-kit-editor/models/section';

import Marker from 'content-kit-editor/models/marker';
import { MARKUP_TYPES } from 'content-kit-editor/models/marker';
import { MARKUP_TAG_NAMES } from 'content-kit-editor/models/marker';
import { getAttributes } from 'content-kit-editor/utils/dom-utils';
import { forEach } from 'content-kit-editor/utils/array-utils';

Expand Down Expand Up @@ -97,10 +97,10 @@ export default {

markupFromElement(element) {
const tagName = element.tagName.toLowerCase();
if (MARKUP_TYPES.indexOf(tagName) === -1) { return null; }
if (MARKUP_TAG_NAMES.indexOf(tagName) === -1) { return null; }

return {
type: tagName,
tagName: tagName,
attributes: getAttributes(element)
};
},
Expand Down
4 changes: 4 additions & 0 deletions src/js/renderers/editor-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ class Visitor {
}
}

section(renderNode, section) {
this.markupSection(renderNode, section);
}

imageSection(renderNode, section) {
if (renderNode.element) {
if (renderNode.element.src !== section.src) {
Expand Down
4 changes: 4 additions & 0 deletions src/js/renderers/mobiledoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ let visitor = {
opcodes.push(['openPost']);
visitArray(visitor, node.sections, opcodes);
},
section(node, opcodes) {
opcodes.push(['openMarkupSection', node.tagName]);
visitArray(visitor, node.markers, opcodes);
},
markupSection(node, opcodes) {
opcodes.push(['openMarkupSection', node.tagName]);
visitArray(visitor, node.markers, opcodes);
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/models/marker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('a marker can have a markup applied to it', (assert) => {
assert.ok(m1.hasMarkup('b'));
});

test('a marker cannot have the same markup type applied twice', (assert) => {
test('a marker cannot have the same markup tagName applied twice', (assert) => {
const m1 = new Marker('hi there!');
m1.addMarkup('b');
m1.addMarkup('b');
Expand All @@ -43,17 +43,17 @@ test('a marker cannot have the same markup type applied twice', (assert) => {

test('a marker can have a complex markup applied to it', (assert) => {
const m1 = new Marker('hi there!');
const markup = {type: 'a', attributes:{href:'blah'}};
const markup = {tagName: 'a', attributes:{href:'blah'}};
m1.addMarkup(markup);

assert.ok(m1.hasMarkup('a'));
assert.equal(m1.getMarkup('a').attributes.href, 'blah');
});

test('a marker cannot have the same complex markup type applied twice, even with different attributes', (assert) => {
test('a marker cannot have the same complex markup tagName applied twice, even with different attributes', (assert) => {
const m1 = new Marker('hi there!');
const markup1 = {type: 'a', attributes:{href:'blah'}};
const markup2 = {type: 'a', attributes:{href:'blah2'}};
const markup1 = {tagName: 'a', attributes:{href:'blah'}};
const markup2 = {tagName: 'a', attributes:{href:'blah2'}};
m1.addMarkup(markup1);
m1.addMarkup(markup2);

Expand Down

0 comments on commit 875d31c

Please sign in to comment.