File size: 2,678 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const compose = require('redux/lib/compose').default;
const { convertToRaw, genKey } = require('draft-js');
const { stateFromMarkdown } = require('draft-js-import-markdown');
const isHtml = require('is-html');
const cheerio = require('cheerio');
const { toPlainText, toState } = require('../../shared/slate-utils');

const convertEmbeds = state => {
  const entityMap = state.entityMap || {};
  const blocks = state.blocks.map(block => {
    if (block.type !== 'unstyled') return block;
    if (!isHtml(block.text)) return block;

    const $ = cheerio.load(block.text);
    const iframe = $('iframe')[0];

    if (!iframe) return block;

    const src = $(iframe).attr('src');
    const height = $(iframe).attr('height');
    const width = $(iframe).attr('width');

    if (!src) return block;

    const keys = Object.keys(entityMap).map(key => parseInt(key, 10));
    const lastKey = keys.sort()[keys.length - 1];
    const newKey = lastKey === undefined ? 0 : lastKey + 1;

    entityMap[newKey] = {
      data: { src, height, width },
      mutability: 'IMMUTABLE',
      type: 'embed',
    };

    return {
      data: {},
      depth: 0,
      entityRanges: [
        {
          key: newKey,
          offset: 0,
          length: 1,
        },
      ],
      inlineStyleRanges: [],
      key: genKey(),
      text: ' ',
      type: 'atomic',
    };
  });

  return {
    ...state,
    entityMap,
    blocks,
  };
};

const plainToDraft = compose(
  JSON.stringify,
  convertEmbeds,
  convertToRaw,
  stateFromMarkdown
);

const slateToDraft = compose(plainToDraft, toPlainText, toState, JSON.parse);

exports.up = function(r, conn) {
  return (
    r
      .table('threads')
      .filter(thread =>
        r.not(thread.hasFields('type')).or(thread('type').ne('DRAFTJS'))
      )
      .run(conn)
      .then(cursor => cursor.toArray())
      // Transform slate state to draftjs state
      .then(threads =>
        threads.map(thread =>
          Object.assign({}, thread, {
            type: 'DRAFTJS',
            content: Object.assign({}, thread.content, {
              body:
                thread.type === 'SLATE'
                  ? slateToDraft(thread.content.body)
                  : plainToDraft(thread.content.body),
            }),
          })
        )
      )
      // Store the transformed threads
      .then(threads =>
        Promise.all(
          threads.map(thread =>
            r
              .table('threads')
              .get(thread.id)
              .update(thread)
              .run(conn)
          )
        )
      )
  );
};

exports.down = function(r, conn) {
  // Not spending any time undoing this
  return Promise.resolve();
};