예제 #1
0
파일: app.py 프로젝트: arslnb/jotpad
def modify_notepad(data):
    Id = data['Id']
    payload = data['delta']
    document = db.reference('/jots/' + Id).get()
    if document:
        oldDoc = Delta(document['ops'])
        change = Delta(payload['ops'])
        composed = oldDoc.compose(change)
        newDoc = {
            "ops": composed.get_ops()
        }
        db.reference('/jots/' + Id).set(newDoc)
        emit('update', {'data': payload['ops'], 'author': request.sid}, room=Id)
    else:
        db.reference('/jots/' + Id).set(payload)
        emit('update', {'data': payload['ops'], 'author': request.sid}, room=Id)
예제 #2
0
	def test_compose(self):
		# tests replicated from: https://github.com/ottypes/rich-text/blob/master/test/delta/compose.js

		# insert + insert
		a = Delta().insert('A')
		b = Delta().insert('B')
		expected = Delta().insert('B').insert('A')
		self.assertEqual(a.compose(b), expected)

		# insert + retain
		a = Delta().insert('A')
		b = Delta().retain(1, {'bold': True, 'color': 'red', 'font': None})
		expected = Delta().insert('A', {'bold': True, 'color': 'red'})
		self.assertEqual(a.compose(b), expected)

		# insert + delete
		a = Delta().insert('A')
		b = Delta().delete(1)
		expected = Delta()
		self.assertEqual(a.compose(b), expected)

		# delete + insert
		a = Delta().delete(1)
		b = Delta().insert('B')
		expected = Delta().insert('B').delete(1)
		self.assertEqual(a.compose(b), expected)

		# delete + retain
		a = Delta().delete(1)
		b = Delta().retain(1, {'bold': True, 'color': 'red'})
		expected = Delta().delete(1).retain(1, {'bold': True, 'color': 'red'})
		self.assertEqual(a.compose(b), expected)

		# delete + delete
		a = Delta().delete(1)
		b = Delta().delete(1)
		expected = Delta().delete(2)
		self.assertEqual(a.compose(b), expected)

		# retain + insert
		a = Delta().retain(1, {'color': 'blue'})
		b = Delta().insert('B')
		expected = Delta().insert('B').retain(1, {'color': 'blue'})
		self.assertEqual(a.compose(b), expected)

		# retain + retain
		a = Delta().retain(1, {'color': 'blue'})
		b = Delta().retain(1, {'bold': True, 'color': 'red', 'font': None})
		expected = Delta().retain(1, {'bold': True, 'color': 'red', 'font': None})
		self.assertEqual(a.compose(b), expected)

		# retain + delete
		a = Delta().retain(1, {'color': 'blue'})
		b = Delta().delete(1)
		expected = Delta().delete(1)
		self.assertEqual(a.compose(b), expected)

		# insert in middle of text
		a = Delta().insert('Hello')
		b = Delta().retain(3).insert('X')
		expected = Delta().insert('HelXlo')
		self.assertEqual(a.compose(b), expected)

		# insert and delete ordering
		a = Delta().insert('Hello')
		b = Delta().insert('Hello')
		insertFirst = Delta().retain(3).insert('X').delete(1)
		deleteFirst = Delta().retain(3).delete(1).insert('X')
		expected = Delta().insert('HelXo')
		self.assertEqual(a.compose(insertFirst), expected)
		self.assertEqual(b.compose(deleteFirst), expected)

		# insert embed
		a = Delta().insert(1, {'src': 'http://quilljs.com/image.png'})
		b = Delta().retain(1, {'alt': 'logo'})
		expected = Delta().insert(1, {'src': 'http://quilljs.com/image.png', 'alt': 'logo'})
		self.assertEqual(a.compose(b), expected)

		# delete entire text
		a = Delta().retain(4).insert('Hello')
		b = Delta().delete(9)
		expected = Delta().delete(4)
		self.assertEqual(a.compose(b), expected)

		# retain more than length of text
		a = Delta().insert('Hello')
		b = Delta().retain(10)
		expected = Delta().insert('Hello')
		self.assertEqual(a.compose(b), expected)

		# retain empty embed
		a = Delta().insert(1)
		b = Delta().retain(1)
		expected = Delta().insert(1)
		self.assertEqual(a.compose(b), expected)

		# remove all attributes
		a = Delta().insert('A', {'bold': True})
		b = Delta().retain(1, {'bold': None})
		expected = Delta().insert('A')
		self.assertEqual(a.compose(b), expected)

		# remove all embed attributes
		a = Delta().insert(2, {'bold': True})
		b = Delta().retain(1, {'bold': None})
		expected = Delta().insert(2)
		self.assertEqual(a.compose(b), expected)

		# immutability
		attr1 = {'bold': True}
		attr2 = {'bold': True}
		a1 = Delta().insert('Test', attr1)
		a2 = Delta().insert('Test', attr1)
		b1 = Delta().retain(1, {'color': 'red'}).delete(2)
		b2 = Delta().retain(1, {'color': 'red'}).delete(2)
		expected = Delta().insert('T', {'color': 'red', 'bold': True}).insert('t', attr1)
		self.assertEqual(a1.compose(b1), expected)
		self.assertEqual(a1, a2)
		self.assertEqual(b1, b2)
		self.assertEqual(attr1, attr2)
예제 #3
0
    def test_compose(self):
        # tests replicated from: https://github.com/ottypes/rich-text/blob/master/test/delta/compose.js

        # insert + insert
        a = Delta().insert('A')
        b = Delta().insert('B')
        expected = Delta().insert('B').insert('A')
        self.assertEqual(a.compose(b), expected)

        # insert + retain
        a = Delta().insert('A')
        b = Delta().retain(1, {'bold': True, 'color': 'red', 'font': None})
        expected = Delta().insert('A', {'bold': True, 'color': 'red'})
        self.assertEqual(a.compose(b), expected)

        # insert + delete
        a = Delta().insert('A')
        b = Delta().delete(1)
        expected = Delta()
        self.assertEqual(a.compose(b), expected)

        # delete + insert
        a = Delta().delete(1)
        b = Delta().insert('B')
        expected = Delta().insert('B').delete(1)
        self.assertEqual(a.compose(b), expected)

        # delete + retain
        a = Delta().delete(1)
        b = Delta().retain(1, {'bold': True, 'color': 'red'})
        expected = Delta().delete(1).retain(1, {'bold': True, 'color': 'red'})
        self.assertEqual(a.compose(b), expected)

        # delete + delete
        a = Delta().delete(1)
        b = Delta().delete(1)
        expected = Delta().delete(2)
        self.assertEqual(a.compose(b), expected)

        # retain + insert
        a = Delta().retain(1, {'color': 'blue'})
        b = Delta().insert('B')
        expected = Delta().insert('B').retain(1, {'color': 'blue'})
        self.assertEqual(a.compose(b), expected)

        # retain + retain
        a = Delta().retain(1, {'color': 'blue'})
        b = Delta().retain(1, {'bold': True, 'color': 'red', 'font': None})
        expected = Delta().retain(1, {
            'bold': True,
            'color': 'red',
            'font': None
        })
        self.assertEqual(a.compose(b), expected)

        # retain + delete
        a = Delta().retain(1, {'color': 'blue'})
        b = Delta().delete(1)
        expected = Delta().delete(1)
        self.assertEqual(a.compose(b), expected)

        # insert in middle of text
        a = Delta().insert('Hello')
        b = Delta().retain(3).insert('X')
        expected = Delta().insert('HelXlo')
        self.assertEqual(a.compose(b), expected)

        # insert and delete ordering
        a = Delta().insert('Hello')
        b = Delta().insert('Hello')
        insertFirst = Delta().retain(3).insert('X').delete(1)
        deleteFirst = Delta().retain(3).delete(1).insert('X')
        expected = Delta().insert('HelXo')
        self.assertEqual(a.compose(insertFirst), expected)
        self.assertEqual(b.compose(deleteFirst), expected)

        # insert embed
        a = Delta().insert(1, {'src': 'http://quilljs.com/image.png'})
        b = Delta().retain(1, {'alt': 'logo'})
        expected = Delta().insert(1, {
            'src': 'http://quilljs.com/image.png',
            'alt': 'logo'
        })
        self.assertEqual(a.compose(b), expected)

        # delete entire text
        a = Delta().retain(4).insert('Hello')
        b = Delta().delete(9)
        expected = Delta().delete(4)
        self.assertEqual(a.compose(b), expected)

        # retain more than length of text
        a = Delta().insert('Hello')
        b = Delta().retain(10)
        expected = Delta().insert('Hello')
        self.assertEqual(a.compose(b), expected)

        # retain empty embed
        a = Delta().insert(1)
        b = Delta().retain(1)
        expected = Delta().insert(1)
        self.assertEqual(a.compose(b), expected)

        # remove all attributes
        a = Delta().insert('A', {'bold': True})
        b = Delta().retain(1, {'bold': None})
        expected = Delta().insert('A')
        self.assertEqual(a.compose(b), expected)

        # remove all embed attributes
        a = Delta().insert(2, {'bold': True})
        b = Delta().retain(1, {'bold': None})
        expected = Delta().insert(2)
        self.assertEqual(a.compose(b), expected)

        # immutability
        attr1 = {'bold': True}
        attr2 = {'bold': True}
        a1 = Delta().insert('Test', attr1)
        a2 = Delta().insert('Test', attr1)
        b1 = Delta().retain(1, {'color': 'red'}).delete(2)
        b2 = Delta().retain(1, {'color': 'red'}).delete(2)
        expected = Delta().insert('T', {
            'color': 'red',
            'bold': True
        }).insert('t', attr1)
        self.assertEqual(a1.compose(b1), expected)
        self.assertEqual(a1, a2)
        self.assertEqual(b1, b2)
        self.assertEqual(attr1, attr2)